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 25 #if defined(CONFIG_SUPPORT_USBPLUG) 26 int _ufs_start(struct ufs_hba *hba); 27 28 static void ufs_info_show_dev_desc(void *buf) 29 { 30 struct ufs_device_descriptor *dev = (struct ufs_device_descriptor *)buf; 31 32 printf("---------------------------\n"); 33 printf("---UFS Device Descriptor---\n"); 34 printf("---------------------------\n"); 35 printf("bLength: 0x%x\n", dev->b_length); 36 printf("bDescriptorIDN: 0x%x\n", dev->b_descriptor_idn); 37 printf("bDevice: 0x%x\n", dev->b_device); 38 printf("bDeviceClass: 0x%x\n", dev->b_device_class); 39 printf("bDeviceSubClass: 0x%x\n", dev->b_device_sub_class); 40 printf("bProtocol: 0x%x\n", dev->b_protocol); 41 printf("bNumberLU: 0x%x\n", dev->b_number_lu); 42 printf("bNumberWLU: 0x%x\n", dev->b_number_wlu); 43 printf("bBootEnable: 0x%x\n", dev->b_boot_enable); 44 printf("bDescrAccessEn: 0x%x\n", dev->b_descr_access_en); 45 printf("bInitPowerMode: 0x%x\n", dev->b_init_power_mode); 46 printf("bHighPriorityLUN: 0x%x\n", dev->b_high_priority_lun); 47 printf("bSecureRemovalType: 0x%x\n", dev->b_secure_removal_type); 48 printf("bSecurityLU: 0x%x\n", dev->b_security_lu); 49 printf("bBackgroundOpsTermLat: 0x%x\n", dev->b_background_ops_term_lat); 50 printf("bInitActiveICCLevel: 0x%x\n", dev->b_init_active_icc_level); 51 printf("wSpecVersion: 0x%x\n", to_bigendian16(dev->w_spec_version)); 52 printf("wManufactureDate: 0x%x\n", to_bigendian16(dev->w_manufacture_date)); 53 printf("iManufacturerName: 0x%x\n", dev->i_manufacturer_name); 54 printf("iProductName: 0x%x\n", dev->i_product_name); 55 printf("iSerialNumber: 0x%x\n", dev->i_serial_number); 56 printf("iOemID: 0x%x\n", dev->i_oem_id); 57 printf("wManufacturerID: 0x%x\n", to_bigendian16(dev->w_manufacturer_id)); 58 printf("bUD0BaseOffset: 0x%x\n", dev->b_ud_0base_offset); 59 printf("bUDConfigPLength: 0x%x\n", dev->b_ud_config_plength); 60 printf("bDeviceRTTCap: 0x%x\n", dev->b_device_rtt_cap); 61 printf("wPeriodicRTCUpdate: 0x%x\n", to_bigendian16(dev->w_periodic_rtc_update)); 62 printf("bUFSFeatureSupport: 0x%x\n", dev->b_ufs_feature_support); 63 printf("bFFUTimeout: 0x%x\n", dev->b_ffu_timeout); 64 printf("bQueueDepth: 0x%x\n", dev->b_queue_depth); 65 printf("wDeviceVersion: 0x%x\n", to_bigendian16(dev->w_device_version)); 66 printf("bNumSecureWPArea: 0x%x\n", dev->b_num_secure_wp_area); 67 printf("dPSAMaxDataSize: 0x%x\n", to_bigendian32(dev->d_psa_max_data_size)); 68 printf("bPSAStateTimeout: 0x%x\n", dev->b_psa_state_timeout); 69 printf("iProductRevisionLevel: 0x%x\n", dev->i_product_revision_level); 70 } 71 72 static void ufs_info_show_conf_desc(void *buf) 73 { 74 struct ufs_configuration_descriptor *c_desc = (struct ufs_configuration_descriptor *)buf; 75 struct ufs_dev_desc_configuration_param *dev; 76 struct ufs_unit_desc_configuration_param *unit; 77 int i; 78 79 dev = &c_desc->dev_desc_conf_param; 80 printf("----------------------------------------\n"); 81 printf("---UFS Device Descriptor Config Param---\n"); 82 printf("----------------------------------------\n"); 83 printf("bLength: 0x%x\n", dev->b_length); 84 printf("bDescriptorIDN: 0x%x\n", dev->b_descriptor_idn); 85 printf("bConfDescContinue: 0x%x\n", dev->b_conf_desc_continue); 86 printf("bBootEnable: 0x%x\n", dev->b_boot_enable); 87 printf("bDescrAccessEn: 0x%x\n", dev->b_descr_access_en); 88 printf("bInitPowerMode: 0x%x\n", dev->b_init_power_mode); 89 printf("bHighPriorityLUN: 0x%x\n", dev->b_high_priority_lun); 90 printf("bSecureRemovalType: 0x%x\n", dev->b_secure_removal_type); 91 printf("bInitActiveICCLevel: 0x%x\n", dev->b_init_active_icc_level); 92 printf("wPeriodicRTCUpdate: 0x%x\n", to_bigendian16(dev->w_periodic_rtc_update)); 93 94 for (i = 0; i < UNIT_DESCS_COUNT; i++) { 95 unit = &c_desc->unit_desc_conf_param[i]; 96 97 printf("-----------------------------------------\n"); 98 printf("---UFS Unit %d Descriptor Config Param---\n", i); 99 printf("-----------------------------------------\n"); 100 printf("bLUEnable: 0x%x\n", unit->b_lu_enable); 101 printf("bBootLunID: 0x%x\n", unit->b_boot_lun_id); 102 printf("bLUWriteProtect: 0x%x\n", unit->b_lu_write_protect); 103 printf("bMemoryType: 0x%x\n", unit->b_memory_type); 104 printf("dNumAllocUnits: 0x%x\n", to_bigendian32(unit->d_num_alloc_units)); 105 printf("bDataReliability: 0x%x\n", unit->b_data_reliability); 106 printf("bLogicalBlockSize: 0x%x\n", unit->b_logical_block_size); 107 printf("bProvisioningType: 0x%x\n", unit->b_provisioning_type); 108 printf("wContextCapabilities: 0x%x\n", to_bigendian16(unit->w_context_capabilities)); 109 } 110 } 111 112 static int ufs_get_configuration_desc(struct ufs_hba *hba, struct ufs_configuration_descriptor *c_desc) 113 { 114 u8 desc_buf[CONFIGURATION_DESC_V22_LENGTH]; 115 u8 *buf = desc_buf; 116 int length = CONFIGURATION_DESC_V22_LENGTH; 117 int err; 118 119 if (CONFIGURATION_DESC_V31_LENGTH == hba->desc_size.conf_desc) { 120 buf = (u8 *)c_desc; 121 length = CONFIGURATION_DESC_V31_LENGTH; 122 } else if(CONFIGURATION_DESC_V22_LENGTH != hba->desc_size.conf_desc) { 123 return -EINVAL; 124 } 125 126 err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_CONFIGURATION, 0, 0, buf, length); 127 if (err) { 128 dev_err(hba->dev, "%s: Failed reading configuration Desc. err = %d\n", 129 __func__, err); 130 return err; 131 } 132 133 if (CONFIGURATION_DESC_V22_LENGTH == hba->desc_size.conf_desc) { 134 memcpy(&c_desc->dev_desc_conf_param, buf, 0x10); 135 buf += 0x10; 136 for (int i = 0; i < UNIT_DESCS_COUNT; i++) { 137 memcpy(&c_desc->unit_desc_conf_param[i], buf, 0x10); 138 buf += 0x10; 139 } 140 } 141 142 return err; 143 } 144 145 static int ufshcd_write_desc_param(struct ufs_hba *hba, enum desc_idn desc_id, 146 int desc_index, u8 param_offset, u8 *param_read_buf, 147 u8 param_size) 148 { 149 int ret; 150 u8 *desc_buf; 151 int buff_len; 152 153 /* Safety check */ 154 if (desc_id >= QUERY_DESC_IDN_MAX || !param_size) 155 return -EINVAL; 156 157 /* Get the max length of descriptor from structure filled up at probe 158 * time. 159 */ 160 ret = ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len); 161 162 /* Sanity checks */ 163 if (ret || !buff_len) { 164 dev_err(hba->dev, "%s: Failed to get full descriptor length\n", 165 __func__); 166 return ret; 167 } 168 169 desc_buf = param_read_buf; 170 /* Request for full descriptor */ 171 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_WRITE_DESC, 172 desc_id, desc_index, 0, desc_buf, 173 &buff_len); 174 175 if (ret) 176 dev_err(hba->dev, "%s: Failed write descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d\n", 177 __func__, desc_id, desc_index, param_offset, ret); 178 179 return ret; 180 } 181 182 static int ufs_write_configuration_desc(struct ufs_hba *hba, struct ufs_configuration_descriptor *c_desc) 183 { 184 u8 desc_buf[CONFIGURATION_DESC_V22_LENGTH]; 185 u8 *buf = desc_buf; 186 int length = CONFIGURATION_DESC_V22_LENGTH; 187 int err; 188 189 if (CONFIGURATION_DESC_V31_LENGTH == hba->desc_size.conf_desc) { 190 buf = (u8 *)c_desc; 191 length = CONFIGURATION_DESC_V31_LENGTH; 192 } else if(CONFIGURATION_DESC_V22_LENGTH == hba->desc_size.conf_desc) { 193 memcpy(buf, &c_desc->dev_desc_conf_param, 0x10); 194 buf += 0x10; 195 for (int i = 0; i < UNIT_DESCS_COUNT; i++) { 196 memcpy(buf, &c_desc->unit_desc_conf_param[i], 0x10); 197 buf += 0x10; 198 } 199 buf = desc_buf; 200 } else { 201 return -EINVAL; 202 } 203 204 err = ufshcd_write_desc_param(hba, QUERY_DESC_IDN_CONFIGURATION, 0, 0, buf, length); 205 if (err) { 206 dev_err(hba->dev, "%s: Failed reading configuration Desc. err = %d\n", 207 __func__, err); 208 return err; 209 } 210 211 return err; 212 } 213 214 static void ufs_lu_configuration(struct ufs_hba *hba, struct ufs_configuration_descriptor *c_desc) 215 { 216 uint32_t denominator = hba->geo_desc->b_allocation_unit_size * to_bigendian32(hba->geo_desc->d_segment_size); 217 struct ufs_dev_desc_configuration_param *dev; 218 struct ufs_unit_desc_configuration_param *unit; 219 uint32_t alloced_units = 0; 220 int i, cap_adj_fac; 221 uint64_t total_raw_device_capacity; 222 223 cap_adj_fac = to_bigendian16(hba->geo_desc->w_enhanced1_cap_adj_fac) / 256; 224 total_raw_device_capacity = cpu_to_be64(hba->geo_desc->q_total_raw_device_capacity); 225 226 dev = &c_desc->dev_desc_conf_param; 227 dev->b_boot_enable = 0x1; 228 dev->b_descr_access_en = 0x0; 229 dev->b_init_power_mode = 0x1; 230 dev->b_high_priority_lun = 0x7F; 231 dev->b_secure_removal_type = 0x0; 232 dev->b_init_active_icc_level = 0x0; 233 dev->w_periodic_rtc_update = 0x0; 234 235 unit = &c_desc->unit_desc_conf_param[0]; 236 /* lu 1: boot lu A 4MB */ 237 unit[1].b_boot_lun_id = WELL_BOOT_LU_A; /* lu 0, boot a */ 238 unit[1].b_memory_type = 0x3; 239 unit[1].d_num_alloc_units = (4 * 0x800 * cap_adj_fac + denominator - 1) / denominator; 240 alloced_units += unit[1].d_num_alloc_units; 241 /* lu 2: boot lu B 4MB */ 242 unit[2].b_boot_lun_id = WELL_BOOT_LU_B; /* lu 1, boot b */ 243 unit[2].b_memory_type = 0x3; /* lu 0, Enhanced Memory */ 244 unit[2].d_num_alloc_units = (4 * 0x800 * cap_adj_fac + denominator - 1) / denominator; 245 alloced_units += unit[2].d_num_alloc_units; 246 /* lu 3: data lu 8MB */ 247 unit[3].b_boot_lun_id = 0x0; /* lu 2 */ 248 unit[3].b_memory_type = 0x3; /* lu 2, Enhanced Memory */ 249 unit[3].d_num_alloc_units = (8 * 0x800 * cap_adj_fac + denominator - 1) / denominator; 250 alloced_units += unit[3].d_num_alloc_units; 251 /* lu 0: data lu, max capacity*/ 252 unit[0].b_boot_lun_id = 0x0; /* lu 3 */ 253 unit[0].b_memory_type = 0x0; /* lu 3, Normal Memory */ 254 unit[0].d_num_alloc_units = lower_32_bits(total_raw_device_capacity) / denominator - alloced_units; 255 256 for (i = 0; i <= 3; i++) { /* lu 0 - 3 */ 257 unit[i].b_lu_enable = 0x1; 258 unit[i].b_lu_write_protect = 0x0; 259 unit[i].b_data_reliability = 0x1; 260 unit[i].b_logical_block_size = 0x0c; 261 unit[i].b_provisioning_type = 0x2; 262 unit[i].w_context_capabilities = 0x0; 263 unit[i].d_num_alloc_units = to_bigendian32(unit[i].d_num_alloc_units); 264 } 265 } 266 267 static int compair_conf_desp(struct ufs_configuration_descriptor *cda, struct ufs_configuration_descriptor *cdb) 268 { 269 struct ufs_dev_desc_configuration_param *dev_a, *dev_b; 270 struct ufs_unit_desc_configuration_param *unit_a, *unit_b; 271 int i, ret; 272 273 dev_a = &cda->dev_desc_conf_param; 274 dev_b = &cdb->dev_desc_conf_param; 275 276 if (dev_a->b_boot_enable != dev_b->b_boot_enable) 277 return 0x3; 278 if (dev_a->b_descr_access_en != dev_b->b_descr_access_en) 279 return 0x4; 280 if (dev_a->b_init_power_mode != dev_b->b_init_power_mode) 281 return 0x5; 282 if (dev_a->b_high_priority_lun != dev_b->b_high_priority_lun) 283 return 0x6; 284 if (dev_a->b_secure_removal_type != dev_b->b_secure_removal_type) 285 return 0x7; 286 if (dev_a->b_init_active_icc_level != dev_b->b_init_active_icc_level) 287 return 0x8; 288 if (dev_a->w_periodic_rtc_update != dev_b->w_periodic_rtc_update) 289 return 0x9; 290 291 for (i = 0; i < UNIT_DESCS_COUNT; i++) { 292 unit_a = &cda->unit_desc_conf_param[i]; 293 unit_b = &cdb->unit_desc_conf_param[i]; 294 295 ret = 0x10 * (i + 1); 296 if (unit_a->b_lu_enable != unit_b->b_lu_enable) 297 return ret; 298 if (unit_a->b_boot_lun_id != unit_b->b_boot_lun_id) 299 return ret + 0x1; 300 if (unit_a->b_lu_write_protect != unit_b->b_lu_write_protect) 301 return ret + 0x2; 302 if (unit_a->b_memory_type != unit_b->b_memory_type) 303 return ret + 0x3; 304 if (unit_a->d_num_alloc_units != unit_b->d_num_alloc_units) 305 return ret + 0x4; 306 if (unit_a->b_data_reliability != unit_b->b_data_reliability) 307 return ret + 0x8; 308 if (unit_a->b_logical_block_size != unit_b->b_logical_block_size) 309 return ret + 0x9; 310 if (unit_a->b_provisioning_type != unit_b->b_provisioning_type) 311 return ret + 0xA; 312 if (unit_a->w_context_capabilities != unit_b->w_context_capabilities) 313 return ret + 0xB; 314 } 315 return 0; 316 } 317 318 static int read_attribute(struct ufs_hba *hba, enum attr_id idn, uint8_t index, uint8_t selector, uint32_t *value) 319 { 320 int ret, buff_len = 4; 321 322 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, 323 idn, index, 0, (uint8_t *)value, &buff_len); 324 return ret; 325 } 326 327 static int write_attribute(struct ufs_hba *hba, enum attr_id idn, uint8_t index, uint8_t selector, uint32_t *value) 328 { 329 int ret, buff_len = 4; 330 331 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, 332 idn, index, 0, (uint8_t *)value, &buff_len); 333 return ret; 334 } 335 336 static int set_boot_lu_enable(struct ufs_hba *hba) 337 { 338 uint32_t value = 0; 339 uint32_t target_value = DEFAULT_BOOT_LUN; 340 int ret; 341 342 ret = read_attribute(hba, B_BOOT_LUNEN, 0, 0, &value); 343 if (ret) { 344 printf("read bBootLunEn fail. ret = %d\n", ret); 345 return ret; 346 } 347 348 if (value != 0) 349 printf("UFS get boot W-LU-%c\n", 350 (value == WELL_BOOT_LU_A) ? 'A' : 'B'); 351 352 if (value == target_value) 353 return 0; 354 355 /* set default boot from Boot LU A */ 356 value = target_value; 357 ret = write_attribute(hba, B_BOOT_LUNEN, 0, 0, &value); 358 if (ret) { 359 printf("write bBootLunEn attribute fail. ret = %d\n", ret); 360 return ret; 361 } 362 363 printf("UFS set boot W-LU(%c)\n", (value == WELL_BOOT_LU_A) ? 'A' : 'B'); 364 return ret; 365 } 366 367 int ufs_create_partition_inventory(struct ufs_hba *hba) 368 { 369 int err, length; 370 371 length = (int)sizeof(struct ufs_geometry_descriptor); 372 if (length > hba->desc_size.geom_desc) 373 length = hba->desc_size.geom_desc; 374 err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_GEOMETRY, 0, 0, (u8 *)hba->geo_desc, length); 375 if (err) { 376 dev_err(hba->dev, "%s: Failed reading geometry Desc. err = %d\n", __func__, err); 377 return err; 378 } 379 380 err = ufs_get_configuration_desc(hba, hba->rc_desc); 381 if (err) { 382 dev_err(hba->dev, "%s: Failed getting conf info. err = %d\n", __func__, err); 383 return err; 384 } 385 ufs_info_show_conf_desc(hba->rc_desc); 386 387 memset(hba->wc_desc, 0, sizeof(struct ufs_configuration_descriptor)); 388 hba->wc_desc->dev_desc_conf_param.b_length = hba->rc_desc->dev_desc_conf_param.b_length; 389 hba->wc_desc->dev_desc_conf_param.b_descriptor_idn = hba->rc_desc->dev_desc_conf_param.b_descriptor_idn; 390 ufs_lu_configuration(hba, hba->wc_desc); 391 ufs_info_show_conf_desc(hba->wc_desc); 392 393 err = compair_conf_desp(hba->wc_desc, hba->rc_desc); 394 printf("compair_conf_desp: 0x%x\n", err); 395 396 if (!err) 397 return 0; 398 399 err = ufs_write_configuration_desc(hba, hba->wc_desc); 400 if (err) 401 dev_err(hba->dev, "%s: Failed write conf info. err = %d\n", __func__, err); 402 403 err = _ufs_start(hba); 404 if (err) 405 return err; 406 407 ufs_info_show_dev_desc(hba->dev_desc); 408 409 return set_boot_lu_enable(hba); 410 } 411 #endif 412