1 /* 2 * Copyright 2017 Rockchip Electronics Co., Ltd 3 * Frank Wang <frank.wang@rock-chips.com> 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 8 #include <asm/io.h> 9 #include <asm/arch/boot_mode.h> 10 #include <asm/arch/chip_info.h> 11 #include <rockusb.h> 12 13 #define ROCKUSB_INTERFACE_CLASS 0xff 14 #define ROCKUSB_INTERFACE_SUB_CLASS 0x06 15 #define ROCKUSB_INTERFACE_PROTOCOL 0x05 16 17 static struct usb_interface_descriptor rkusb_intf_desc = { 18 .bLength = USB_DT_INTERFACE_SIZE, 19 .bDescriptorType = USB_DT_INTERFACE, 20 .bInterfaceNumber = 0x00, 21 .bAlternateSetting = 0x00, 22 .bNumEndpoints = 0x02, 23 .bInterfaceClass = ROCKUSB_INTERFACE_CLASS, 24 .bInterfaceSubClass = ROCKUSB_INTERFACE_SUB_CLASS, 25 .bInterfaceProtocol = ROCKUSB_INTERFACE_PROTOCOL, 26 }; 27 28 static struct usb_descriptor_header *rkusb_fs_function[] = { 29 (struct usb_descriptor_header *)&rkusb_intf_desc, 30 (struct usb_descriptor_header *)&fsg_fs_bulk_in_desc, 31 (struct usb_descriptor_header *)&fsg_fs_bulk_out_desc, 32 NULL, 33 }; 34 35 static struct usb_descriptor_header *rkusb_hs_function[] = { 36 (struct usb_descriptor_header *)&rkusb_intf_desc, 37 (struct usb_descriptor_header *)&fsg_hs_bulk_in_desc, 38 (struct usb_descriptor_header *)&fsg_hs_bulk_out_desc, 39 NULL, 40 }; 41 42 struct rk_flash_info { 43 u32 flash_size; 44 u16 block_size; 45 u8 page_size; 46 u8 ecc_bits; 47 u8 access_time; 48 u8 manufacturer; 49 u8 flash_mask; 50 } __packed; 51 52 static int rkusb_rst_code; /* The subcode in reset command (0xFF) */ 53 54 int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name) 55 { 56 if (IS_RKUSB_UMS_DNL(name)) { 57 /* Fix to Rockchip's VID and PID */ 58 dev->idVendor = __constant_cpu_to_le16(0x2207); 59 dev->idProduct = __constant_cpu_to_le16(CONFIG_ROCKUSB_G_DNL_PID); 60 61 /* Enumerate as a loader device */ 62 dev->bcdUSB = cpu_to_le16(0x0201); 63 } else if (!strncmp(name, "usb_dnl_fastboot", 16)) { 64 /* Fix to Google's VID and PID */ 65 dev->idVendor = __constant_cpu_to_le16(0x18d1); 66 dev->idProduct = __constant_cpu_to_le16(0xd00d); 67 } 68 69 return 0; 70 } 71 72 __maybe_unused 73 static inline void dump_cbw(struct fsg_bulk_cb_wrap *cbw) 74 { 75 assert(!cbw); 76 77 debug("%s:\n", __func__); 78 debug("Signature %x\n", cbw->Signature); 79 debug("Tag %x\n", cbw->Tag); 80 debug("DataTransferLength %x\n", cbw->DataTransferLength); 81 debug("Flags %x\n", cbw->Flags); 82 debug("LUN %x\n", cbw->Lun); 83 debug("Length %x\n", cbw->Length); 84 debug("OptionCode %x\n", cbw->CDB[0]); 85 debug("SubCode %x\n", cbw->CDB[1]); 86 debug("SectorAddr %x\n", get_unaligned_be32(&cbw->CDB[2])); 87 debug("BlkSectors %x\n\n", get_unaligned_be16(&cbw->CDB[7])); 88 } 89 90 static int rkusb_check_lun(struct fsg_common *common) 91 { 92 struct fsg_lun *curlun; 93 94 /* Check the LUN */ 95 if (common->lun >= 0 && common->lun < common->nluns) { 96 curlun = &common->luns[common->lun]; 97 if (common->cmnd[0] != SC_REQUEST_SENSE) { 98 curlun->sense_data = SS_NO_SENSE; 99 curlun->info_valid = 0; 100 } 101 } else { 102 curlun = NULL; 103 common->bad_lun_okay = 0; 104 105 /* 106 * INQUIRY and REQUEST SENSE commands are explicitly allowed 107 * to use unsupported LUNs; all others may not. 108 */ 109 if (common->cmnd[0] != SC_INQUIRY && 110 common->cmnd[0] != SC_REQUEST_SENSE) { 111 debug("unsupported LUN %d\n", common->lun); 112 return -EINVAL; 113 } 114 } 115 116 return 0; 117 } 118 119 static void __do_reset(struct usb_ep *ep, struct usb_request *req) 120 { 121 u32 boot_flag = BOOT_NORMAL; 122 123 if (rkusb_rst_code == 0x03) 124 boot_flag = BOOT_BROM_DOWNLOAD; 125 126 rkusb_rst_code = 0; /* restore to default */ 127 writel(boot_flag, (void *)CONFIG_ROCKCHIP_BOOT_MODE_REG); 128 129 do_reset(NULL, 0, 0, NULL); 130 } 131 132 static int rkusb_do_reset(struct fsg_common *common, 133 struct fsg_buffhd *bh) 134 { 135 common->data_size_from_cmnd = common->cmnd[4]; 136 common->residue = 0; 137 bh->inreq->complete = __do_reset; 138 bh->state = BUF_STATE_EMPTY; 139 140 rkusb_rst_code = !common->cmnd[1] ? 0xff : common->cmnd[1]; 141 return 0; 142 } 143 144 static int rkusb_do_test_unit_ready(struct fsg_common *common, 145 struct fsg_buffhd *bh) 146 { 147 common->residue = 0x06 << 24; /* Max block xfer support from host */ 148 common->data_dir = DATA_DIR_NONE; 149 bh->state = BUF_STATE_EMPTY; 150 151 return 0; 152 } 153 154 static int rkusb_do_read_flash_id(struct fsg_common *common, 155 struct fsg_buffhd *bh) 156 { 157 u8 *buf = (u8 *)bh->buf; 158 u32 len = common->data_size; 159 enum if_type type = ums[common->lun].block_dev.if_type; 160 161 if (type == IF_TYPE_MMC) 162 memcpy((void *)&buf[0], "EMMC ", 5); 163 else if (type == IF_TYPE_RKNAND) 164 memcpy((void *)&buf[0], "NAND ", 5); 165 else 166 memcpy((void *)&buf[0], "UNKN ", 5); /* unknown */ 167 168 /* Set data xfer size */ 169 common->residue = common->data_size_from_cmnd = len; 170 171 return len; 172 } 173 174 static int rkusb_do_test_bad_block(struct fsg_common *common, 175 struct fsg_buffhd *bh) 176 { 177 u8 *buf = (u8 *)bh->buf; 178 u32 len = common->data_size; 179 180 memset((void *)&buf[0], 0, len); 181 182 /* Set data xfer size */ 183 common->residue = common->data_size_from_cmnd = len; 184 185 return len; 186 } 187 188 static int rkusb_do_read_flash_info(struct fsg_common *common, 189 struct fsg_buffhd *bh) 190 { 191 u8 *buf = (u8 *)bh->buf; 192 u32 len = sizeof(struct rk_flash_info); 193 struct rk_flash_info finfo = { 194 .block_size = 1024, 195 .ecc_bits = 0, 196 .page_size = 4, 197 .access_time = 40, 198 .manufacturer = 0, 199 .flash_mask = 0 200 }; 201 202 finfo.flash_size = (u32)ums[common->lun].block_dev.lba; 203 if (finfo.flash_size) 204 finfo.flash_mask = 1; 205 206 memset((void *)&buf[0], 0, len); 207 memcpy((void *)&buf[0], (void *)&finfo, len); 208 209 /* Set data xfer size */ 210 common->residue = common->data_size_from_cmnd = len; 211 /* legacy upgrade_tool does not set correct transfer size */ 212 common->data_size = len; 213 214 return len; 215 } 216 217 static int rkusb_do_get_chip_info(struct fsg_common *common, 218 struct fsg_buffhd *bh) 219 { 220 u8 *buf = (u8 *)bh->buf; 221 u32 len = common->data_size; 222 u32 chip_info[4]; 223 224 memset((void *)chip_info, 0, sizeof(chip_info)); 225 rockchip_rockusb_get_chip_info(chip_info); 226 227 memset((void *)&buf[0], 0, len); 228 memcpy((void *)&buf[0], (void *)chip_info, len); 229 230 /* Set data xfer size */ 231 common->residue = common->data_size_from_cmnd = len; 232 233 return len; 234 } 235 236 static int rkusb_do_lba_erase(struct fsg_common *common, 237 struct fsg_buffhd *bh) 238 { 239 struct fsg_lun *curlun = &common->luns[common->lun]; 240 u32 lba, amount; 241 loff_t file_offset; 242 int rc; 243 244 lba = get_unaligned_be32(&common->cmnd[2]); 245 if (lba >= curlun->num_sectors) { 246 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 247 rc = -EINVAL; 248 goto out; 249 } 250 251 file_offset = ((loff_t) lba) << 9; 252 amount = get_unaligned_be16(&common->cmnd[7]) << 9; 253 if (unlikely(amount == 0)) { 254 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 255 rc = -EIO; 256 goto out; 257 } 258 259 /* Perform the erase */ 260 rc = ums[common->lun].erase_sector(&ums[common->lun], 261 file_offset / SECTOR_SIZE, 262 amount / SECTOR_SIZE); 263 if (!rc) { 264 curlun->sense_data = SS_MEDIUM_NOT_PRESENT; 265 rc = -EIO; 266 } 267 268 out: 269 common->data_dir = DATA_DIR_NONE; 270 bh->state = BUF_STATE_EMPTY; 271 272 return rc; 273 } 274 275 static int rkusb_do_read_capacity(struct fsg_common *common, 276 struct fsg_buffhd *bh) 277 { 278 u8 *buf = (u8 *)bh->buf; 279 u32 len = common->data_size; 280 enum if_type type = ums[common->lun].block_dev.if_type; 281 282 /* 283 * bit[0]: Direct LBA, 0: Disabled; 284 * bit[1]: Vendor Storage API, 0: default; 285 * bit[2]: First 4M Access, 0: Disabled; 286 * bit[3:63}: Reserved. 287 */ 288 memset((void *)&buf[0], 0, len); 289 buf[0] = (type == IF_TYPE_MMC) ? (BIT(2) | BIT(0)) : BIT(0); 290 291 /* Set data xfer size */ 292 common->residue = common->data_size_from_cmnd = len; 293 294 return len; 295 } 296 297 static void rkusb_fixup_cbwcb(struct fsg_common *common, 298 struct fsg_buffhd *bh) 299 { 300 struct usb_request *req = bh->outreq; 301 struct fsg_bulk_cb_wrap *cbw = req->buf; 302 303 /* FIXME cbw.DataTransferLength was not set by Upgrade Tool */ 304 common->data_size = le32_to_cpu(cbw->DataTransferLength); 305 if (common->data_size == 0) { 306 common->data_size = 307 get_unaligned_be16(&common->cmnd[7]) << 9; 308 printf("Trasfer Length NOT set, please use new version tool\n"); 309 debug("%s %d, cmnd1 %x\n", __func__, 310 get_unaligned_be16(&common->cmnd[7]), 311 get_unaligned_be16(&common->cmnd[1])); 312 } 313 if (cbw->Flags & USB_BULK_IN_FLAG) 314 common->data_dir = DATA_DIR_TO_HOST; 315 else 316 common->data_dir = DATA_DIR_FROM_HOST; 317 318 /* Not support */ 319 common->cmnd[1] = 0; 320 } 321 322 static int rkusb_cmd_process(struct fsg_common *common, 323 struct fsg_buffhd *bh, int *reply) 324 { 325 struct usb_request *req = bh->outreq; 326 struct fsg_bulk_cb_wrap *cbw = req->buf; 327 int rc; 328 329 dump_cbw(cbw); 330 331 rkusb_fixup_cbwcb(common, bh); 332 if (rkusb_check_lun(common)) { 333 *reply = -EINVAL; 334 return RKUSB_RC_ERROR; 335 } 336 337 switch (common->cmnd[0]) { 338 case RKUSB_TEST_UNIT_READY: 339 *reply = rkusb_do_test_unit_ready(common, bh); 340 rc = RKUSB_RC_FINISHED; 341 break; 342 343 case RKUSB_READ_FLASH_ID: 344 *reply = rkusb_do_read_flash_id(common, bh); 345 rc = RKUSB_RC_FINISHED; 346 break; 347 348 case RKUSB_TEST_BAD_BLOCK: 349 *reply = rkusb_do_test_bad_block(common, bh); 350 rc = RKUSB_RC_FINISHED; 351 break; 352 353 case RKUSB_LBA_READ_10: 354 common->cmnd[0] = SC_READ_10; 355 common->cmnd[1] = 0; /* Not support */ 356 rc = RKUSB_RC_CONTINUE; 357 break; 358 359 case RKUSB_LBA_WRITE_10: 360 common->cmnd[0] = SC_WRITE_10; 361 common->cmnd[1] = 0; /* Not support */ 362 rc = RKUSB_RC_CONTINUE; 363 break; 364 365 case RKUSB_READ_FLASH_INFO: 366 *reply = rkusb_do_read_flash_info(common, bh); 367 rc = RKUSB_RC_FINISHED; 368 break; 369 370 case RKUSB_GET_CHIP_VER: 371 *reply = rkusb_do_get_chip_info(common, bh); 372 rc = RKUSB_RC_FINISHED; 373 break; 374 375 case RKUSB_LBA_ERASE: 376 *reply = rkusb_do_lba_erase(common, bh); 377 rc = RKUSB_RC_FINISHED; 378 break; 379 380 case RKUSB_READ_CAPACITY: 381 *reply = rkusb_do_read_capacity(common, bh); 382 rc = RKUSB_RC_FINISHED; 383 break; 384 385 case RKUSB_RESET: 386 *reply = rkusb_do_reset(common, bh); 387 rc = RKUSB_RC_FINISHED; 388 break; 389 390 case RKUSB_SET_DEVICE_ID: 391 case RKUSB_READ_10: 392 case RKUSB_WRITE_10: 393 case RKUSB_ERASE_10: 394 case RKUSB_WRITE_SPARE: 395 case RKUSB_READ_SPARE: 396 case RKUSB_ERASE_10_FORCE: 397 case RKUSB_GET_VERSION: 398 case RKUSB_ERASE_SYS_DISK: 399 case RKUSB_SDRAM_READ_10: 400 case RKUSB_SDRAM_WRITE_10: 401 case RKUSB_SDRAM_EXECUTE: 402 case RKUSB_LOW_FORMAT: 403 case RKUSB_SET_RESET_FLAG: 404 case RKUSB_SPI_READ_10: 405 case RKUSB_SPI_WRITE_10: 406 case RKUSB_SESSION: 407 /* Fall through */ 408 default: 409 rc = RKUSB_RC_UNKNOWN_CMND; 410 break; 411 } 412 413 return rc; 414 } 415 416 DECLARE_GADGET_BIND_CALLBACK(rkusb_ums_dnl, fsg_add); 417