xref: /rk3399_rockchip-uboot/drivers/ufs/ufs-rockchip-usbplug.c (revision a4719b90cc2f09e5348b830d61f32ab6d991069a)
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 #include <charset.h>
9 #include <common.h>
10 #include <dm.h>
11 #include <log.h>
12 #include <dm/lists.h>
13 #include <dm/device-internal.h>
14 #include <malloc.h>
15 #include <hexdump.h>
16 #include <scsi.h>
17 #include <asm/io.h>
18 #include <asm/dma-mapping.h>
19 #include <linux/bitops.h>
20 #include <linux/delay.h>
21 
22 #include "ufs-rockchip-usbplug.h"
23 #include "ufs.h"
24 /* Query request retries */
25 #define QUERY_REQ_RETRIES 3
26 /* Query request timeout */
27 #define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */
28 
29 #if defined(CONFIG_SUPPORT_USBPLUG)
30 int _ufs_start(struct ufs_hba *hba);
31 
32 static void ufs_info_show_dev_desc(void *buf)
33 {
34 	struct ufs_device_descriptor *dev = (struct ufs_device_descriptor *)buf;
35 
36 	printf("---------------------------\n");
37 	printf("---UFS Device Descriptor---\n");
38 	printf("---------------------------\n");
39 	printf("bLength: 0x%x\n", dev->b_length);
40 	printf("bDescriptorIDN: 0x%x\n", dev->b_descriptor_idn);
41 	printf("bDevice: 0x%x\n", dev->b_device);
42 	printf("bDeviceClass: 0x%x\n", dev->b_device_class);
43 	printf("bDeviceSubClass: 0x%x\n", dev->b_device_sub_class);
44 	printf("bProtocol: 0x%x\n", dev->b_protocol);
45 	printf("bNumberLU: 0x%x\n", dev->b_number_lu);
46 	printf("bNumberWLU: 0x%x\n", dev->b_number_wlu);
47 	printf("bBootEnable: 0x%x\n", dev->b_boot_enable);
48 	printf("bDescrAccessEn: 0x%x\n", dev->b_descr_access_en);
49 	printf("bInitPowerMode: 0x%x\n", dev->b_init_power_mode);
50 	printf("bHighPriorityLUN: 0x%x\n", dev->b_high_priority_lun);
51 	printf("bSecureRemovalType: 0x%x\n", dev->b_secure_removal_type);
52 	printf("bSecurityLU: 0x%x\n", dev->b_security_lu);
53 	printf("bBackgroundOpsTermLat: 0x%x\n", dev->b_background_ops_term_lat);
54 	printf("bInitActiveICCLevel: 0x%x\n", dev->b_init_active_icc_level);
55 	printf("wSpecVersion: 0x%x\n", to_bigendian16(dev->w_spec_version));
56 	printf("wManufactureDate: 0x%x\n", to_bigendian16(dev->w_manufacture_date));
57 	printf("iManufacturerName: 0x%x\n", dev->i_manufacturer_name);
58 	printf("iProductName: 0x%x\n", dev->i_product_name);
59 	printf("iSerialNumber: 0x%x\n", dev->i_serial_number);
60 	printf("iOemID: 0x%x\n", dev->i_oem_id);
61 	printf("wManufacturerID: 0x%x\n", to_bigendian16(dev->w_manufacturer_id));
62 	printf("bUD0BaseOffset: 0x%x\n", dev->b_ud_0base_offset);
63 	printf("bUDConfigPLength: 0x%x\n", dev->b_ud_config_plength);
64 	printf("bDeviceRTTCap: 0x%x\n", dev->b_device_rtt_cap);
65 	printf("wPeriodicRTCUpdate: 0x%x\n", to_bigendian16(dev->w_periodic_rtc_update));
66 	printf("bUFSFeatureSupport: 0x%x\n", dev->b_ufs_feature_support);
67 	printf("bFFUTimeout: 0x%x\n", dev->b_ffu_timeout);
68 	printf("bQueueDepth: 0x%x\n", dev->b_queue_depth);
69 	printf("wDeviceVersion: 0x%x\n", to_bigendian16(dev->w_device_version));
70 	printf("bNumSecureWPArea: 0x%x\n", dev->b_num_secure_wp_area);
71 	printf("dPSAMaxDataSize: 0x%x\n", to_bigendian32(dev->d_psa_max_data_size));
72 	printf("bPSAStateTimeout: 0x%x\n", dev->b_psa_state_timeout);
73 	printf("iProductRevisionLevel: 0x%x\n", dev->i_product_revision_level);
74 }
75 
76 static void ufs_info_show_conf_desc(void *buf)
77 {
78 	struct ufs_configuration_descriptor *c_desc = (struct ufs_configuration_descriptor *)buf;
79 	struct ufs_dev_desc_configuration_param *dev;
80 	struct ufs_unit_desc_configuration_param *unit;
81 	int i;
82 
83 	dev = &c_desc->dev_desc_conf_param;
84 	printf("----------------------------------------\n");
85 	printf("---UFS Device Descriptor Config Param---\n");
86 	printf("----------------------------------------\n");
87 	printf("bLength: 0x%x\n", dev->b_length);
88 	printf("bDescriptorIDN: 0x%x\n", dev->b_descriptor_idn);
89 	printf("bConfDescContinue: 0x%x\n", dev->b_conf_desc_continue);
90 	printf("bBootEnable: 0x%x\n", dev->b_boot_enable);
91 	printf("bDescrAccessEn: 0x%x\n", dev->b_descr_access_en);
92 	printf("bInitPowerMode: 0x%x\n", dev->b_init_power_mode);
93 	printf("bHighPriorityLUN: 0x%x\n", dev->b_high_priority_lun);
94 	printf("bSecureRemovalType: 0x%x\n", dev->b_secure_removal_type);
95 	printf("bInitActiveICCLevel: 0x%x\n", dev->b_init_active_icc_level);
96 	printf("wPeriodicRTCUpdate: 0x%x\n", to_bigendian16(dev->w_periodic_rtc_update));
97 	printf("bSecureRemovalType: 0x%x\n", dev->b_secure_removal_type);
98 	printf("bInitActiveICCLevel: 0x%x\n", dev->b_init_active_icc_level);
99 	printf("wPeriodicRTCUpdate: 0x%x\n", to_bigendian16(dev->w_periodic_rtc_update));
100 	printf("bWB_EN: 0x%x\n", dev->b_write_booster_buffer_reserve_user_space_en);
101 	printf("WB_TYPE: 0x%x\n", dev->b_write_booster_buffer_type);
102 	printf("WB_alloc_units: 0x%x\n", to_bigendian32(dev->d_num_shared_write_booster_buffer_alloc_units));
103 
104 	for (i = 0; i < UNIT_DESCS_COUNT; i++) {
105 		unit = &c_desc->unit_desc_conf_param[i];
106 
107 		printf("-----------------------------------------\n");
108 		printf("---UFS Unit %d Descriptor Config Param---\n", i);
109 		printf("-----------------------------------------\n");
110 		printf("bLUEnable: 0x%x\n", unit->b_lu_enable);
111 		printf("bBootLunID: 0x%x\n", unit->b_boot_lun_id);
112 		printf("bLUWriteProtect: 0x%x\n", unit->b_lu_write_protect);
113 		printf("bMemoryType: 0x%x\n", unit->b_memory_type);
114 		printf("dNumAllocUnits: 0x%x\n", to_bigendian32(unit->d_num_alloc_units));
115 		printf("bDataReliability: 0x%x\n", unit->b_data_reliability);
116 		printf("bLogicalBlockSize: 0x%x\n", unit->b_logical_block_size);
117 		printf("bProvisioningType: 0x%x\n", unit->b_provisioning_type);
118 		printf("wContextCapabilities: 0x%x\n", to_bigendian16(unit->w_context_capabilities));
119 	}
120 }
121 
122 static int ufs_get_configuration_desc(struct ufs_hba *hba, struct ufs_configuration_descriptor *c_desc)
123 {
124 	u8 desc_buf[CONFIGURATION_DESC_V22_LENGTH];
125 	u8 *buf = desc_buf;
126 	int length = CONFIGURATION_DESC_V22_LENGTH;
127 	int err;
128 
129 	if (CONFIGURATION_DESC_V31_LENGTH == hba->desc_size.conf_desc) {
130 		buf = (u8 *)c_desc;
131 		length = CONFIGURATION_DESC_V31_LENGTH;
132 	} else if(CONFIGURATION_DESC_V22_LENGTH != hba->desc_size.conf_desc) {
133 		return -EINVAL;
134 	}
135 
136 	err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_CONFIGURATION, 0, 0, buf, length);
137 	if (err) {
138 		dev_err(hba->dev, "%s: Failed reading configuration Desc. err = %d\n",
139 			__func__, err);
140 		return err;
141 	}
142 
143 	if (CONFIGURATION_DESC_V22_LENGTH == hba->desc_size.conf_desc) {
144 		memcpy(&c_desc->dev_desc_conf_param, buf, 0x10);
145 		buf += 0x10;
146 		for (int i = 0; i < UNIT_DESCS_COUNT; i++) {
147 			memcpy(&c_desc->unit_desc_conf_param[i], buf,  0x10);
148 			buf += 0x10;
149 		}
150 	}
151 
152 	return err;
153 }
154 
155 static int ufshcd_write_desc_param(struct ufs_hba *hba, enum desc_idn desc_id,
156 			   int desc_index, u8 param_offset, u8 *param_read_buf,
157 			   u8 param_size)
158 {
159 	int ret;
160 	u8 *desc_buf;
161 	int buff_len;
162 
163 	/* Safety check */
164 	if (desc_id >= QUERY_DESC_IDN_MAX || !param_size)
165 		return -EINVAL;
166 
167 	/* Get the max length of descriptor from structure filled up at probe
168 	 * time.
169 	 */
170 	ret = ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len);
171 
172 	/* Sanity checks */
173 	if (ret || !buff_len) {
174 		dev_err(hba->dev, "%s: Failed to get full descriptor length\n",
175 			__func__);
176 		return ret;
177 	}
178 
179 	desc_buf = param_read_buf;
180 	/* Request for full descriptor */
181 	ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_WRITE_DESC,
182 					    desc_id, desc_index, 0, desc_buf,
183 					    &buff_len);
184 
185 	if (ret)
186 		dev_err(hba->dev, "%s: Failed write descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d\n",
187 			__func__, desc_id, desc_index, param_offset, ret);
188 
189 	return ret;
190 }
191 
192 static int ufs_write_configuration_desc(struct ufs_hba *hba, struct ufs_configuration_descriptor *c_desc)
193 {
194 	u8 desc_buf[CONFIGURATION_DESC_V22_LENGTH];
195 	u8 *buf = desc_buf;
196 	int length = CONFIGURATION_DESC_V22_LENGTH;
197 	int err;
198 
199 	if (CONFIGURATION_DESC_V31_LENGTH == hba->desc_size.conf_desc) {
200 		buf = (u8 *)c_desc;
201 		length = CONFIGURATION_DESC_V31_LENGTH;
202 	} else if(CONFIGURATION_DESC_V22_LENGTH == hba->desc_size.conf_desc) {
203 		memcpy(buf, &c_desc->dev_desc_conf_param, 0x10);
204 		buf += 0x10;
205 		for (int i = 0; i < UNIT_DESCS_COUNT; i++) {
206 			memcpy(buf, &c_desc->unit_desc_conf_param[i], 0x10);
207 			buf += 0x10;
208 		}
209 		buf = desc_buf;
210 	} else {
211 		return -EINVAL;
212 	}
213 
214 	err = ufshcd_write_desc_param(hba, QUERY_DESC_IDN_CONFIGURATION, 0, 0, buf, length);
215 	if (err) {
216 		dev_err(hba->dev, "%s: Failed reading configuration Desc. err = %d\n",
217 			__func__, err);
218 		return err;
219 	}
220 
221 	return err;
222 }
223 
224 static void ufs_lu_configuration(struct ufs_hba *hba, struct ufs_configuration_descriptor *c_desc)
225 {
226 	uint32_t denominator = hba->geo_desc->b_allocation_unit_size * to_bigendian32(hba->geo_desc->d_segment_size);
227 	struct ufs_dev_desc_configuration_param *dev;
228 	struct ufs_unit_desc_configuration_param *unit;
229 	uint32_t alloced_units = 0;
230 	int i, cap_adj_fac;
231 	uint64_t total_raw_device_capacity;
232 	uint32_t max_wb_alloc_units = cpu_to_be32(hba->geo_desc->d_write_booster_buffer_max_alloc_units);
233 	uint32_t wb_alloc_units;
234 
235 	cap_adj_fac = to_bigendian16(hba->geo_desc->w_enhanced1_cap_adj_fac) / 256;
236 	total_raw_device_capacity = cpu_to_be64(hba->geo_desc->q_total_raw_device_capacity);
237 
238 	dev = &c_desc->dev_desc_conf_param;
239 	dev->b_boot_enable = 0x1;
240 	dev->b_descr_access_en = 0x0;
241 	dev->b_init_power_mode = 0x1;
242 	dev->b_high_priority_lun = 0x7F;
243 	dev->b_secure_removal_type = 0x0;
244 	dev->b_init_active_icc_level = 0x0;
245 	dev->w_periodic_rtc_update = 0x0;
246 
247 	unit = &c_desc->unit_desc_conf_param[0];
248 	/* lu 1: boot lu A 4MB */
249 	unit[1].b_boot_lun_id = WELL_BOOT_LU_A; /* lu 0, boot a */
250 	unit[1].b_memory_type = 0x3;
251 	unit[1].d_num_alloc_units = (4 * 0x800 * cap_adj_fac + denominator - 1) / denominator;
252 	alloced_units += unit[1].d_num_alloc_units;
253 	/* lu 2: boot lu B 4MB */
254 	unit[2].b_boot_lun_id = WELL_BOOT_LU_B; /* lu 1, boot b */
255 	unit[2].b_memory_type = 0x3; /* lu 0, Enhanced Memory */
256 	unit[2].d_num_alloc_units = (4 * 0x800 * cap_adj_fac + denominator - 1) / denominator;
257 	alloced_units += unit[2].d_num_alloc_units;
258 	/* lu 3: data lu 8MB */
259 	unit[3].b_boot_lun_id = 0x0; /* lu 2 */
260 	unit[3].b_memory_type = 0x3; /* lu 2, Enhanced Memory */
261 	unit[3].d_num_alloc_units = (8 * 0x800 * cap_adj_fac + denominator - 1) / denominator;
262 	alloced_units += unit[3].d_num_alloc_units;
263 
264 	if (max_wb_alloc_units) {
265 		wb_alloc_units = max_wb_alloc_units;
266 		if (wb_alloc_units > max_wb_alloc_units)
267 			wb_alloc_units = max_wb_alloc_units;
268 		dev->b_write_booster_buffer_reserve_user_space_en = 1;
269 		dev->b_write_booster_buffer_type = 1;
270 		dev->d_num_shared_write_booster_buffer_alloc_units = to_bigendian32(wb_alloc_units);
271 	}
272 
273 	/* lu 0: data lu, max capacity*/
274 	unit[0].b_boot_lun_id = 0x0; /* lu 3 */
275 	unit[0].b_memory_type = 0x0; /* lu 3, Normal Memory */
276 	unit[0].d_num_alloc_units = lower_32_bits(total_raw_device_capacity) / denominator - alloced_units;
277 
278 	for (i = 0; i <= 3; i++) { /* lu 0 - 3 */
279 		unit[i].b_lu_enable = 0x1;
280 		unit[i].b_lu_write_protect = 0x0;
281 		unit[i].b_data_reliability = 0x1;
282 		unit[i].b_logical_block_size = 0x0c;
283 		unit[i].b_provisioning_type = 0x2;
284 		unit[i].w_context_capabilities = 0x0;
285 		unit[i].d_num_alloc_units = to_bigendian32(unit[i].d_num_alloc_units);
286 	}
287 }
288 
289 static int compair_conf_desp(struct ufs_configuration_descriptor *cda, struct ufs_configuration_descriptor *cdb)
290 {
291 	struct ufs_dev_desc_configuration_param *dev_a, *dev_b;
292 	struct ufs_unit_desc_configuration_param *unit_a, *unit_b;
293 	int i, ret;
294 
295 	dev_a = &cda->dev_desc_conf_param;
296 	dev_b = &cdb->dev_desc_conf_param;
297 
298 	if (dev_a->b_boot_enable != dev_b->b_boot_enable)
299 		return 0x3;
300 	if (dev_a->b_descr_access_en != dev_b->b_descr_access_en)
301 		return 0x4;
302 	if (dev_a->b_init_power_mode != dev_b->b_init_power_mode)
303 		return 0x5;
304 	if (dev_a->b_high_priority_lun != dev_b->b_high_priority_lun)
305 		return 0x6;
306 	if (dev_a->b_secure_removal_type != dev_b->b_secure_removal_type)
307 		return 0x7;
308 	if (dev_a->b_init_active_icc_level != dev_b->b_init_active_icc_level)
309 		return 0x8;
310 	if (dev_a->w_periodic_rtc_update != dev_b->w_periodic_rtc_update)
311 		return 0x9;
312 	if (dev_a->b_write_booster_buffer_reserve_user_space_en !=
313 	    dev_b->b_write_booster_buffer_reserve_user_space_en)
314 		return 0xA;
315 	if (dev_a->b_write_booster_buffer_type != dev_b->b_write_booster_buffer_type)
316 		return 0xB;
317 	if (dev_a->d_num_shared_write_booster_buffer_alloc_units !=
318 	    dev_b->d_num_shared_write_booster_buffer_alloc_units)
319 		return 0xC;
320 
321 	for (i = 0; i < UNIT_DESCS_COUNT; i++) {
322 		unit_a = &cda->unit_desc_conf_param[i];
323 		unit_b = &cdb->unit_desc_conf_param[i];
324 
325 		ret = 0x10 * (i + 1);
326 		if (unit_a->b_lu_enable != unit_b->b_lu_enable)
327 			return ret;
328 		if (unit_a->b_boot_lun_id != unit_b->b_boot_lun_id)
329 			return ret + 0x1;
330 		if (unit_a->b_lu_write_protect != unit_b->b_lu_write_protect)
331 			return ret + 0x2;
332 		if (unit_a->b_memory_type != unit_b->b_memory_type)
333 			return ret + 0x3;
334 		if (unit_a->d_num_alloc_units != unit_b->d_num_alloc_units)
335 			return ret + 0x4;
336 		if (unit_a->b_data_reliability != unit_b->b_data_reliability)
337 			return ret + 0x8;
338 		if (unit_a->b_logical_block_size != unit_b->b_logical_block_size)
339 			return ret + 0x9;
340 		if (unit_a->b_provisioning_type != unit_b->b_provisioning_type)
341 			return ret + 0xA;
342 		if (unit_a->w_context_capabilities != unit_b->w_context_capabilities)
343 			return ret + 0xB;
344 	}
345 	return 0;
346 }
347 
348 
349 /**
350  * ufshcd_init_query() - init the query response and request parameters
351  */
352 static inline void ufshcd_init_query(struct ufs_hba *hba,
353 				     struct ufs_query_req **request,
354 				     struct ufs_query_res **response,
355 				     enum query_opcode opcode,
356 				     u8 idn, u8 index, u8 selector)
357 {
358 	*request = &hba->dev_cmd.query.request;
359 	*response = &hba->dev_cmd.query.response;
360 	memset(*request, 0, sizeof(struct ufs_query_req));
361 	memset(*response, 0, sizeof(struct ufs_query_res));
362 	(*request)->upiu_req.opcode = opcode;
363 	(*request)->upiu_req.idn = idn;
364 	(*request)->upiu_req.index = index;
365 	(*request)->upiu_req.selector = selector;
366 }
367 
368 /**
369  * ufshcd_query_flag() - API function for sending flag query requests
370  */
371 static int ufshcd_query_attribute(struct ufs_hba *hba,enum query_opcode opcode,
372 				  enum attr_id idn, u8 index, u8 selector, u32 *value)
373 {
374 	struct ufs_query_req *request = &hba->dev_cmd.query.request;
375 	struct ufs_query_res *response = &hba->dev_cmd.query.response;
376 	int err;
377 	int timeout = QUERY_REQ_TIMEOUT;
378 
379 	memset(request, 0, sizeof(struct ufs_query_req));
380 	memset(response, 0, sizeof(struct ufs_query_res));
381 	request->upiu_req.opcode = opcode;
382 	request->upiu_req.idn = idn;
383 	request->upiu_req.index = 0;
384 	request->upiu_req.selector = 0;
385 
386 	switch (opcode) {
387 	case UPIU_QUERY_OPCODE_WRITE_ATTR:
388 		request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
389 		request->upiu_req.value = be32_to_cpu(*value);
390 		break;
391 	case UPIU_QUERY_OPCODE_READ_ATTR:
392 		request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
393 		break;
394 	default:
395 		dev_err(hba->dev,
396 			"%s: Expected query flag opcode but got = %d\n",
397 			__func__, opcode);
398 		err = -EINVAL;
399 		goto out;
400 	}
401 
402 	err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
403 
404 	if (err) {
405 		dev_err(hba->dev,
406 			"%s: Sending flag query for idn %d failed, err = %d\n",
407 			__func__, idn, err);
408 		goto out;
409 	}
410 
411 	if (value)
412 		*value = be32_to_cpu(response->upiu_res.value);
413 
414 out:
415 	return err;
416 }
417 
418 static int ufshcd_query_attribute_retry(struct ufs_hba *hba, enum query_opcode opcode,
419 					enum attr_id idn, u8 index, u8 selector, u32 *value)
420 {
421 	int ret;
422 	int retries;
423 
424 	for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
425 		ret = ufshcd_query_attribute(hba, opcode, idn, index, selector, value);
426 		if (ret)
427 			dev_dbg(hba->dev,
428 				"%s: failed with error %d, retries %d\n",
429 				__func__, ret, retries);
430 		else
431 			break;
432 	}
433 
434 	if (ret)
435 		dev_err(hba->dev,
436 			"%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
437 			__func__, opcode, idn, ret, retries);
438 	return ret;
439 }
440 
441 static int read_attribute(struct ufs_hba *hba, enum attr_id idn, u8 index, u8 selector, u32 *value)
442 {
443 	int ret;
444 
445 	ret = ufshcd_query_attribute_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
446 					    idn, index, 0, value);
447 	return ret;
448 }
449 
450 static int write_attribute(struct ufs_hba *hba, enum attr_id idn, u8 index, u8 selector, u32 *value)
451 {
452 	int ret;
453 
454 	ret = ufshcd_query_attribute_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
455 					    idn, index, 0, value);
456 	return ret;
457 }
458 
459 static int set_boot_lu_enable(struct ufs_hba *hba)
460 {
461 	uint32_t value = 0;
462 	uint32_t target_value = DEFAULT_BOOT_LUN;
463 	int ret;
464 
465 	ret = read_attribute(hba, B_BOOT_LUNEN, 0, 0, &value);
466 	if (ret) {
467 		printf("read bBootLunEn fail. ret = %d\n", ret);
468 		return ret;
469 	}
470 
471 	if (value != 0)
472 		printf("UFS get boot W-LU-%c\n", (value == WELL_BOOT_LU_A) ? 'A' : 'B');
473 
474 	if (value == target_value)
475 		return 0;
476 
477 	/* set default boot from Boot LU A */
478 	value = target_value;
479 	ret = write_attribute(hba, B_BOOT_LUNEN, 0, 0, &value);
480 	if (ret) {
481 		printf("write bBootLunEn attribute fail. ret = %d\n", ret);
482 		return ret;
483 	}
484 
485 	ret = read_attribute(hba, B_BOOT_LUNEN, 0, 0, &value);
486 	if (ret) {
487 		printf("read bBootLunEn fail. ret = %d\n", ret);
488 		return ret;
489 	}
490 
491 	if (target_value == value)
492 		return 0;
493 
494 	printf("UFS set boot W-LU(%c) Fail value = %x\n", (value == WELL_BOOT_LU_A) ? 'A' : 'B', value);
495 	return 0;
496 }
497 
498 static int ufs_set_ref_clk(struct ufs_hba *hba)
499 {
500 	uint32_t value;
501 	int ret;
502 	uint32_t target_ref_clk;
503 
504 	target_ref_clk = 1; /* 26 MHz */
505 
506 	ret = read_attribute(hba, B_REFCLK_FREQ, 0, 0, &value);
507 	if (ret) {
508 		printf("read bRefClkFreq fail. ret = %d\n", ret);
509 		return ret;
510 	}
511 
512 	printf("UFS get ref clock %d Mhz\n", (value == 1) ? 26 : 19);
513 	if (target_ref_clk == value)
514 		return 0;
515 
516 	/* set default boot from Boot LU A */
517 	ret = write_attribute(hba, B_REFCLK_FREQ, 0, 0, &target_ref_clk);
518 	if (ret) {
519 		printf("write bRefClkFreq attribute fail. ret = %d\n", ret);
520 		return ret;
521 	}
522 
523 	ret = read_attribute(hba, B_REFCLK_FREQ, 0, 0, &value);
524 	if (ret) {
525 		printf("read bRefClkFreq fail. ret = %d\n", ret);
526 		return ret;
527 	}
528 
529 	if (target_ref_clk == value)
530 		return 0;
531 
532 	printf("UFS set bRefClkFreq  26Mhz Fail\n");
533 	return -EINVAL;
534 }
535 
536 int ufs_create_partition_inventory(struct ufs_hba *hba)
537 {
538 	int err, length;
539 
540 	length = (int)sizeof(struct ufs_geometry_descriptor);
541 	if (length > hba->desc_size.geom_desc)
542 		length = hba->desc_size.geom_desc;
543 	err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_GEOMETRY, 0, 0, (u8 *)hba->geo_desc, length);
544 	if (err) {
545 		dev_err(hba->dev, "%s: Failed reading geometry Desc. err = %d\n", __func__, err);
546 		return err;
547 	}
548 
549 	dev_err(hba->dev, "%s: WB_max_alloc_units = %x\n", __func__,
550 		hba->geo_desc->d_write_booster_buffer_max_alloc_units);
551 
552 	err = ufs_get_configuration_desc(hba, hba->rc_desc);
553 	if (err) {
554 		dev_err(hba->dev, "%s: Failed getting conf info. err = %d\n", __func__, err);
555 		return err;
556 	}
557 	ufs_info_show_conf_desc(hba->rc_desc);
558 
559 	memset(hba->wc_desc, 0, sizeof(struct ufs_configuration_descriptor));
560 	hba->wc_desc->dev_desc_conf_param.b_length = hba->rc_desc->dev_desc_conf_param.b_length;
561 	hba->wc_desc->dev_desc_conf_param.b_descriptor_idn = hba->rc_desc->dev_desc_conf_param.b_descriptor_idn;
562 	ufs_lu_configuration(hba, hba->wc_desc);
563 	ufs_info_show_conf_desc(hba->wc_desc);
564 
565 	err = compair_conf_desp(hba->wc_desc, hba->rc_desc);
566 	printf("compair_conf_desp: 0x%x\n", err);
567 
568 	if (!err)
569 		goto out;
570 
571 	err = ufs_write_configuration_desc(hba, hba->wc_desc);
572 	if (err)
573 		dev_err(hba->dev, "%s: Failed write conf info. err = %d\n", __func__, err);
574 
575 	err =  _ufs_start(hba);
576         if (err)
577                 return err;
578 out:
579 	ufs_info_show_dev_desc(hba->dev_desc);
580 
581 	ufs_set_ref_clk(hba);
582 
583         return set_boot_lu_enable(hba);
584 }
585 #endif
586