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 = common->data_size; 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 212 return len; 213 } 214 215 static int rkusb_do_get_chip_info(struct fsg_common *common, 216 struct fsg_buffhd *bh) 217 { 218 u8 *buf = (u8 *)bh->buf; 219 u32 len = common->data_size; 220 u32 chip_info[4]; 221 222 memset((void *)chip_info, 0, sizeof(chip_info)); 223 rockchip_rockusb_get_chip_info(chip_info); 224 225 memset((void *)&buf[0], 0, len); 226 memcpy((void *)&buf[0], (void *)chip_info, len); 227 228 /* Set data xfer size */ 229 common->residue = common->data_size_from_cmnd = len; 230 231 return len; 232 } 233 234 static int rkusb_do_lba_erase(struct fsg_common *common, 235 struct fsg_buffhd *bh) 236 { 237 struct fsg_lun *curlun = &common->luns[common->lun]; 238 u32 lba, amount; 239 loff_t file_offset; 240 int rc; 241 242 lba = get_unaligned_be32(&common->cmnd[2]); 243 if (lba >= curlun->num_sectors) { 244 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 245 rc = -EINVAL; 246 goto out; 247 } 248 249 file_offset = ((loff_t) lba) << 9; 250 amount = get_unaligned_be16(&common->cmnd[7]) << 9; 251 if (unlikely(amount == 0)) { 252 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 253 rc = -EIO; 254 goto out; 255 } 256 257 /* Perform the erase */ 258 rc = ums[common->lun].erase_sector(&ums[common->lun], 259 file_offset / SECTOR_SIZE, 260 amount / SECTOR_SIZE); 261 if (!rc) { 262 curlun->sense_data = SS_MEDIUM_NOT_PRESENT; 263 rc = -EIO; 264 } 265 266 out: 267 common->data_dir = DATA_DIR_NONE; 268 bh->state = BUF_STATE_EMPTY; 269 270 return rc; 271 } 272 273 static int rkusb_do_read_capacity(struct fsg_common *common, 274 struct fsg_buffhd *bh) 275 { 276 u8 *buf = (u8 *)bh->buf; 277 u32 len = common->data_size; 278 enum if_type type = ums[common->lun].block_dev.if_type; 279 280 /* 281 * bit[0]: Direct LBA, 0: Disabled; 282 * bit[1]: Vendor Storage API, 0: default; 283 * bit[2]: First 4M Access, 0: Disabled; 284 * bit[3:63}: Reserved. 285 */ 286 memset((void *)&buf[0], 0, len); 287 buf[0] = (type == IF_TYPE_MMC) ? (BIT(2) | BIT(0)) : BIT(0); 288 289 /* Set data xfer size */ 290 common->residue = common->data_size_from_cmnd = len; 291 292 return len; 293 } 294 295 static int rkusb_cmd_process(struct fsg_common *common, 296 struct fsg_buffhd *bh, int *reply) 297 { 298 struct usb_request *req = bh->outreq; 299 struct fsg_bulk_cb_wrap *cbw = req->buf; 300 int rc; 301 302 dump_cbw(cbw); 303 304 if (rkusb_check_lun(common)) { 305 *reply = -EINVAL; 306 return RKUSB_RC_ERROR; 307 } 308 309 switch (common->cmnd[0]) { 310 case RKUSB_TEST_UNIT_READY: 311 *reply = rkusb_do_test_unit_ready(common, bh); 312 rc = RKUSB_RC_FINISHED; 313 break; 314 315 case RKUSB_READ_FLASH_ID: 316 *reply = rkusb_do_read_flash_id(common, bh); 317 rc = RKUSB_RC_FINISHED; 318 break; 319 320 case RKUSB_TEST_BAD_BLOCK: 321 *reply = rkusb_do_test_bad_block(common, bh); 322 rc = RKUSB_RC_FINISHED; 323 break; 324 325 case RKUSB_LBA_READ_10: 326 common->cmnd[0] = SC_READ_10; 327 common->cmnd[1] = 0; /* Not support */ 328 rc = RKUSB_RC_CONTINUE; 329 break; 330 331 case RKUSB_LBA_WRITE_10: 332 common->cmnd[0] = SC_WRITE_10; 333 common->cmnd[1] = 0; /* Not support */ 334 rc = RKUSB_RC_CONTINUE; 335 break; 336 337 case RKUSB_READ_FLASH_INFO: 338 *reply = rkusb_do_read_flash_info(common, bh); 339 rc = RKUSB_RC_FINISHED; 340 break; 341 342 case RKUSB_GET_CHIP_VER: 343 *reply = rkusb_do_get_chip_info(common, bh); 344 rc = RKUSB_RC_FINISHED; 345 break; 346 347 case RKUSB_LBA_ERASE: 348 *reply = rkusb_do_lba_erase(common, bh); 349 rc = RKUSB_RC_FINISHED; 350 break; 351 352 case RKUSB_READ_CAPACITY: 353 *reply = rkusb_do_read_capacity(common, bh); 354 rc = RKUSB_RC_FINISHED; 355 break; 356 357 case RKUSB_RESET: 358 *reply = rkusb_do_reset(common, bh); 359 rc = RKUSB_RC_FINISHED; 360 break; 361 362 case RKUSB_SET_DEVICE_ID: 363 case RKUSB_READ_10: 364 case RKUSB_WRITE_10: 365 case RKUSB_ERASE_10: 366 case RKUSB_WRITE_SPARE: 367 case RKUSB_READ_SPARE: 368 case RKUSB_ERASE_10_FORCE: 369 case RKUSB_GET_VERSION: 370 case RKUSB_ERASE_SYS_DISK: 371 case RKUSB_SDRAM_READ_10: 372 case RKUSB_SDRAM_WRITE_10: 373 case RKUSB_SDRAM_EXECUTE: 374 case RKUSB_LOW_FORMAT: 375 case RKUSB_SET_RESET_FLAG: 376 case RKUSB_SPI_READ_10: 377 case RKUSB_SPI_WRITE_10: 378 case RKUSB_SESSION: 379 /* Fall through */ 380 default: 381 rc = RKUSB_RC_UNKNOWN_CMND; 382 break; 383 } 384 385 return rc; 386 } 387 388 DECLARE_GADGET_BIND_CALLBACK(rkusb_ums_dnl, fsg_add); 389