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