blob: 286c014a9f1fa3044b44c31228ce9d397846f7d6 [file] [log] [blame]
Haojian Zhuangfffe9e72016-03-18 22:08:26 +08001/*
Qixiang Xuede9a192018-01-17 13:31:21 +08002 * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved.
Haojian Zhuangfffe9e72016-03-18 22:08:26 +08003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Haojian Zhuangfffe9e72016-03-18 22:08:26 +08005 */
6
7#ifndef __EMMC_H__
8#define __EMMC_H__
9
10#include <stdint.h>
11
12#define EMMC_BLOCK_SIZE 512
13#define EMMC_BLOCK_MASK (EMMC_BLOCK_SIZE - 1)
14#define EMMC_BOOT_CLK_RATE (400 * 1000)
15
16#define EMMC_CMD0 0
17#define EMMC_CMD1 1
18#define EMMC_CMD2 2
19#define EMMC_CMD3 3
20#define EMMC_CMD6 6
21#define EMMC_CMD7 7
22#define EMMC_CMD8 8
23#define EMMC_CMD9 9
24#define EMMC_CMD12 12
25#define EMMC_CMD13 13
26#define EMMC_CMD17 17
27#define EMMC_CMD18 18
Qixiang Xuede9a192018-01-17 13:31:21 +080028#define EMMC_CMD21 21
Haojian Zhuangbd5cf9c2016-08-02 20:51:27 +080029#define EMMC_CMD23 23
Haojian Zhuangfffe9e72016-03-18 22:08:26 +080030#define EMMC_CMD24 24
31#define EMMC_CMD25 25
32#define EMMC_CMD35 35
33#define EMMC_CMD36 36
34#define EMMC_CMD38 38
35
36#define OCR_POWERUP (1 << 31)
37#define OCR_BYTE_MODE (0 << 29)
38#define OCR_SECTOR_MODE (2 << 29)
39#define OCR_ACCESS_MODE_MASK (3 << 29)
40#define OCR_VDD_MIN_2V7 (0x1ff << 15)
41#define OCR_VDD_MIN_2V0 (0x7f << 8)
42#define OCR_VDD_MIN_1V7 (1 << 7)
43
44#define EMMC_RESPONSE_R1 1
45#define EMMC_RESPONSE_R1B 1
46#define EMMC_RESPONSE_R2 4
47#define EMMC_RESPONSE_R3 1
48#define EMMC_RESPONSE_R4 1
49#define EMMC_RESPONSE_R5 1
50
51#define EMMC_FIX_RCA 6 /* > 1 */
52#define RCA_SHIFT_OFFSET 16
53
54#define CMD_EXTCSD_PARTITION_CONFIG 179
55#define CMD_EXTCSD_BUS_WIDTH 183
56#define CMD_EXTCSD_HS_TIMING 185
57
58#define PART_CFG_BOOT_PARTITION1_ENABLE (1 << 3)
59#define PART_CFG_PARTITION1_ACCESS (1 << 0)
60
61/* values in EXT CSD register */
62#define EMMC_BUS_WIDTH_1 0
63#define EMMC_BUS_WIDTH_4 1
64#define EMMC_BUS_WIDTH_8 2
Qixiang Xuede9a192018-01-17 13:31:21 +080065#define EMMC_BUS_WIDTH_DDR_4 5
66#define EMMC_BUS_WIDTH_DDR_8 6
Haojian Zhuangfffe9e72016-03-18 22:08:26 +080067#define EMMC_BOOT_MODE_BACKWARD (0 << 3)
68#define EMMC_BOOT_MODE_HS_TIMING (1 << 3)
69#define EMMC_BOOT_MODE_DDR (2 << 3)
70
71#define EXTCSD_SET_CMD (0 << 24)
72#define EXTCSD_SET_BITS (1 << 24)
73#define EXTCSD_CLR_BITS (2 << 24)
74#define EXTCSD_WRITE_BYTES (3 << 24)
75#define EXTCSD_CMD(x) (((x) & 0xff) << 16)
76#define EXTCSD_VALUE(x) (((x) & 0xff) << 8)
77
78#define STATUS_CURRENT_STATE(x) (((x) & 0xf) << 9)
79#define STATUS_READY_FOR_DATA (1 << 8)
80#define STATUS_SWITCH_ERROR (1 << 7)
81#define EMMC_GET_STATE(x) (((x) >> 9) & 0xf)
82#define EMMC_STATE_IDLE 0
83#define EMMC_STATE_READY 1
84#define EMMC_STATE_IDENT 2
85#define EMMC_STATE_STBY 3
86#define EMMC_STATE_TRAN 4
87#define EMMC_STATE_DATA 5
88#define EMMC_STATE_RCV 6
89#define EMMC_STATE_PRG 7
90#define EMMC_STATE_DIS 8
91#define EMMC_STATE_BTST 9
92#define EMMC_STATE_SLP 10
93
Haojian Zhuangbd5cf9c2016-08-02 20:51:27 +080094#define EMMC_FLAG_CMD23 (1 << 0)
95
Haojian Zhuangfffe9e72016-03-18 22:08:26 +080096typedef struct emmc_cmd {
97 unsigned int cmd_idx;
98 unsigned int cmd_arg;
99 unsigned int resp_type;
100 unsigned int resp_data[4];
101} emmc_cmd_t;
102
103typedef struct emmc_ops {
104 void (*init)(void);
105 int (*send_cmd)(emmc_cmd_t *cmd);
106 int (*set_ios)(int clk, int width);
107 int (*prepare)(int lba, uintptr_t buf, size_t size);
108 int (*read)(int lba, uintptr_t buf, size_t size);
109 int (*write)(int lba, const uintptr_t buf, size_t size);
110} emmc_ops_t;
111
112typedef struct emmc_csd {
Qixiang.Xue28ca9c2016-11-17 11:58:18 +0800113 unsigned int not_used: 1;
114 unsigned int crc: 7;
115 unsigned int ecc: 2;
116 unsigned int file_format: 2;
117 unsigned int tmp_write_protect: 1;
118 unsigned int perm_write_protect: 1;
119 unsigned int copy: 1;
120 unsigned int file_format_grp: 1;
Haojian Zhuangfffe9e72016-03-18 22:08:26 +0800121
Qixiang.Xue28ca9c2016-11-17 11:58:18 +0800122 unsigned int reserved_1: 5;
123 unsigned int write_bl_partial: 1;
124 unsigned int write_bl_len: 4;
125 unsigned int r2w_factor: 3;
126 unsigned int default_ecc: 2;
127 unsigned int wp_grp_enable: 1;
Haojian Zhuangfffe9e72016-03-18 22:08:26 +0800128
129 unsigned int wp_grp_size: 5;
130 unsigned int erase_grp_mult: 5;
131 unsigned int erase_grp_size: 5;
132 unsigned int c_size_mult: 3;
133 unsigned int vdd_w_curr_max: 3;
134 unsigned int vdd_w_curr_min: 3;
135 unsigned int vdd_r_curr_max: 3;
136 unsigned int vdd_r_curr_min: 3;
137 unsigned int c_size_low: 2;
138
139 unsigned int c_size_high: 10;
140 unsigned int reserved_2: 2;
141 unsigned int dsr_imp: 1;
142 unsigned int read_blk_misalign: 1;
143 unsigned int write_blk_misalign: 1;
144 unsigned int read_bl_partial: 1;
145 unsigned int read_bl_len: 4;
146 unsigned int ccc: 12;
147
148 unsigned int tran_speed: 8;
149 unsigned int nsac: 8;
150 unsigned int taac: 8;
151 unsigned int reserved_3: 2;
152 unsigned int spec_vers: 4;
153 unsigned int csd_structure: 2;
154} emmc_csd_t;
155
156size_t emmc_read_blocks(int lba, uintptr_t buf, size_t size);
157size_t emmc_write_blocks(int lba, const uintptr_t buf, size_t size);
158size_t emmc_erase_blocks(int lba, size_t size);
159size_t emmc_rpmb_read_blocks(int lba, uintptr_t buf, size_t size);
160size_t emmc_rpmb_write_blocks(int lba, const uintptr_t buf, size_t size);
161size_t emmc_rpmb_erase_blocks(int lba, size_t size);
Haojian Zhuangbd5cf9c2016-08-02 20:51:27 +0800162void emmc_init(const emmc_ops_t *ops, int clk, int bus_width,
163 unsigned int flags);
Haojian Zhuangfffe9e72016-03-18 22:08:26 +0800164
165#endif /* __EMMC_H__ */