xref: /rk3399_rockchip-uboot/drivers/ufs/ufs-rockchip-usbplug.h (revision 6a8f377ca2e7df7257118262bdf7e8e1412915ef)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Rockchip UFS Host Controller driver
4  *
5  * Copyright (C) 2024 Rockchip Electronics Co.Ltd.
6  */
7 
8 #define WELL_BOOT_LU_A		0x01
9 #define WELL_BOOT_LU_B		0x02
10 
11 #define DEFAULT_BOOT_LUN	WELL_BOOT_LU_A
12 #define DEFAULT_ACTIVE_LUN	0
13 
14 #define CONFIGURATION_DESC_V31_LENGTH	0xE6
15 #define CONFIGURATION_DESC_V30_LENGTH	0xE2
16 #define CONFIGURATION_DESC_V22_LENGTH	0x90
17 #define UNIT_DESCS_COUNT		8
18 
19 /* Byte swap u16 */
20 static inline uint16_t swap_16(uint16_t val)
21 {
22 	return (uint16_t)((val << 8) | (val >> 8)); /* shift 8 */
23 }
24 
25 /* Byte swap unsigned int */
26 static inline uint32_t swap_32(uint32_t val)
27 {
28 	val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF); /* shift 8 */
29 	return (uint32_t)((val << 16) | (val >> 16)); /* shift 16 */
30 }
31 
32 static inline uint16_t to_bigendian16(uint16_t val)
33 {
34 #ifdef HOST_BIG_ENDIAN
35 	return val;
36 #else
37 	return swap_16(val);
38 #endif
39 }
40 
41 static inline uint32_t to_bigendian32(uint32_t val)
42 {
43 #ifdef HOST_BIG_ENDIAN
44 	return val;
45 #else
46 	return swap_32(val);
47 #endif
48 }
49 
50 enum attr_id {
51 	B_BOOT_LUNEN = 0x0,
52 	B_CURRENT_PM = 0x2,
53 	B_ACTIV_ICC_LEVEL = 0x3,
54 	B_OUT_OF_ORDER_DATAEN = 0x4,
55 	B_BCKGND_OPS_STATUS = 0x5,
56 	B_PURGE_STATUS = 0x6,
57 	B_MAX_DATA_IN_SIZE = 0x7,
58 	B_MAX_DATA_OUT_SIZE = 0x8,
59 	D_DYN_CAP_NEEDED = 0x9,
60 	B_REFCLK_FREQ = 0xA,
61 	B_CONFIG_DESC_LOCK = 0xB,
62 	B_MAX_NUM_OF_RTT = 0xC,
63 	W_EXCEPTION_EVENT_CONTROL = 0xD,
64 	W_EXCEPTION_EVENT_STATUS = 0xE,
65 	D_SECONDS_PASSED = 0xF,
66 	W_CONTEXT_CONF = 0x10,
67 	D_CORR_PRG_BLKNUM = 0x11
68 };
69 
70 struct ufs_dev_desc_configuration_param {
71 	uint8_t b_length;
72 	uint8_t b_descriptor_idn;
73 	uint8_t b_conf_desc_continue;
74 	uint8_t b_boot_enable;
75 	uint8_t b_descr_access_en;
76 	uint8_t b_init_power_mode;
77 	uint8_t b_high_priority_lun;
78 	uint8_t b_secure_removal_type;
79 	uint8_t b_init_active_icc_level;
80 	uint16_t w_periodic_rtc_update;
81 	uint8_t reserved[5]; /* 5 reserved, 11 in ufs3.1 */
82 	uint8_t b_write_booster_buffer_reserve_user_space_en;
83 	uint8_t b_write_booster_buffer_type;
84 	uint32_t d_num_shared_write_booster_buffer_alloc_units;
85 } __attribute__ ((packed));
86 
87 struct ufs_unit_desc_configuration_param {
88 	uint8_t b_lu_enable;
89 	uint8_t b_boot_lun_id;
90 	uint8_t b_lu_write_protect;
91 	uint8_t b_memory_type;
92 	uint32_t d_num_alloc_units;
93 	uint8_t b_data_reliability;
94 	uint8_t b_logical_block_size;
95 	uint8_t b_provisioning_type;
96 	uint16_t w_context_capabilities;
97 	uint8_t reserved[13]; /* 3 reserved, 13 in ufs3.1 */
98 } __attribute__ ((packed));
99 
100 struct ufs_configuration_descriptor {
101 	struct ufs_dev_desc_configuration_param dev_desc_conf_param;
102 	struct ufs_unit_desc_configuration_param unit_desc_conf_param[UNIT_DESCS_COUNT];
103 } __attribute__ ((packed));
104 
105 struct ufs_geometry_descriptor {
106 	uint8_t b_length;
107 	uint8_t b_descriptor_idn;
108 	uint8_t b_media_technology;
109 	uint8_t reserved;
110 	uint64_t q_total_raw_device_capacity;
111 	uint8_t b_max_number_lu;
112 	uint32_t d_segment_size;
113 	uint8_t b_allocation_unit_size;
114 	uint8_t b_min_addr_block_size;
115 	uint8_t b_optimal_read_block_size;
116 	uint8_t b_optimal_write_block_size;
117 	uint8_t b_max_in_buffer_size;
118 	uint8_t b_max_out_buffer_size;
119 	uint8_t b_rpmb_read_write_size;
120 	uint8_t b_dynamic_capacity_resource_policy;
121 	uint8_t b_data_ordering;
122 	uint8_t b_max_contex_id_number;
123 	uint8_t b_sys_data_tag_unit_size;
124 	uint8_t b_sys_data_tag_res_size;
125 	uint8_t b_supported_sec_rtypes;
126 	uint16_t w_supported_memory_types;
127 	uint32_t d_system_code_max_alloc_u;
128 	uint16_t w_system_code_cap_adj_fac;
129 	uint32_t d_non_persist_max_alloc_u;
130 	uint16_t w_non_persist_cap_adj_fac;
131 	uint32_t d_enhanced1_max_alloc_u;
132 	uint16_t w_enhanced1_cap_adj_fac;
133 	uint32_t d_enhanced2_max_alloc_u;
134 	uint16_t w_enhanced2_cap_adj_fac;
135 	uint32_t d_enhanced3_max_alloc_u;
136 	uint16_t w_enhanced3_cap_adj_fac;
137 	uint32_t d_enhanced4_max_alloc_u;
138 	uint16_t w_enhanced4_cap_adj_fac;
139 	uint32_t d_optimal_logical_block_size;
140 	/* 15 reserved in ufs3.1 */
141 	uint8_t b_hpb_region_size;
142 	uint8_t b_hpb_number_lu;
143 	uint8_t b_hpb_sub_region_size;
144 	uint16_t w_device_max_active_hpb_regions;
145 	uint16_t w_eserved;
146 	uint32_t d_write_booster_buffer_max_alloc_units;
147 	uint8_t b_device_max_write_booster_lus;
148 	uint8_t b_write_booster_buffer_cap_adj_fac;
149 	uint8_t b_supported_write_booster_buffer_user_space_reduction_types;
150 	uint8_t b_supported_write_booster_buffer_types;
151 } __attribute__ ((packed));
152