xref: /rk3399_rockchip-uboot/drivers/usb/gadget/f_rockusb.c (revision e7b5bb3cc9527752c2c01acb4325fc0721fb75aa)
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 = 5;
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 	common->data_size = len;
171 
172 	return len;
173 }
174 
175 static int rkusb_do_test_bad_block(struct fsg_common *common,
176 				   struct fsg_buffhd *bh)
177 {
178 	u8 *buf = (u8 *)bh->buf;
179 	u32 len = 64;
180 
181 	memset((void *)&buf[0], 0, len);
182 
183 	/* Set data xfer size */
184 	common->residue = common->data_size_from_cmnd = len;
185 	common->data_size = len;
186 
187 	return len;
188 }
189 
190 static int rkusb_do_read_flash_info(struct fsg_common *common,
191 				    struct fsg_buffhd *bh)
192 {
193 	u8 *buf = (u8 *)bh->buf;
194 	u32 len = sizeof(struct rk_flash_info);
195 	struct rk_flash_info finfo = {
196 		.block_size = 1024,
197 		.ecc_bits = 0,
198 		.page_size = 4,
199 		.access_time = 40,
200 		.manufacturer = 0,
201 		.flash_mask = 0
202 	};
203 
204 	finfo.flash_size = (u32)ums[common->lun].block_dev.lba;
205 	if (finfo.flash_size)
206 		finfo.flash_mask = 1;
207 
208 	memset((void *)&buf[0], 0, len);
209 	memcpy((void *)&buf[0], (void *)&finfo, len);
210 
211 	/* Set data xfer size */
212 	common->residue = common->data_size_from_cmnd = len;
213         /* legacy upgrade_tool does not set correct transfer size */
214 	common->data_size = len;
215 
216 	return len;
217 }
218 
219 static int rkusb_do_get_chip_info(struct fsg_common *common,
220 				  struct fsg_buffhd *bh)
221 {
222 	u8 *buf = (u8 *)bh->buf;
223 	u32 len = common->data_size;
224 	u32 chip_info[4];
225 
226 	memset((void *)chip_info, 0, sizeof(chip_info));
227 	rockchip_rockusb_get_chip_info(chip_info);
228 
229 	memset((void *)&buf[0], 0, len);
230 	memcpy((void *)&buf[0], (void *)chip_info, len);
231 
232 	/* Set data xfer size */
233 	common->residue = common->data_size_from_cmnd = len;
234 
235 	return len;
236 }
237 
238 static int rkusb_do_lba_erase(struct fsg_common *common,
239 			      struct fsg_buffhd *bh)
240 {
241 	struct fsg_lun *curlun = &common->luns[common->lun];
242 	u32 lba, amount;
243 	loff_t file_offset;
244 	int rc;
245 
246 	lba = get_unaligned_be32(&common->cmnd[2]);
247 	if (lba >= curlun->num_sectors) {
248 		curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
249 		rc = -EINVAL;
250 		goto out;
251 	}
252 
253 	file_offset = ((loff_t) lba) << 9;
254 	amount = get_unaligned_be16(&common->cmnd[7]) << 9;
255 	if (unlikely(amount == 0)) {
256 		curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
257 		rc = -EIO;
258 		goto out;
259 	}
260 
261 	/* Perform the erase */
262 	rc = ums[common->lun].erase_sector(&ums[common->lun],
263 			       file_offset / SECTOR_SIZE,
264 			       amount / SECTOR_SIZE);
265 	if (!rc) {
266 		curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
267 		rc = -EIO;
268 	}
269 
270 out:
271 	common->data_dir = DATA_DIR_NONE;
272 	bh->state = BUF_STATE_EMPTY;
273 
274 	return rc;
275 }
276 
277 static int rkusb_do_read_capacity(struct fsg_common *common,
278 				    struct fsg_buffhd *bh)
279 {
280 	u8 *buf = (u8 *)bh->buf;
281 	u32 len = common->data_size;
282 	enum if_type type = ums[common->lun].block_dev.if_type;
283 
284 	/*
285 	 * bit[0]: Direct LBA, 0: Disabled;
286 	 * bit[1]: Vendor Storage API, 0: default;
287 	 * bit[2]: First 4M Access, 0: Disabled;
288 	 * bit[3:63}: Reserved.
289 	 */
290 	memset((void *)&buf[0], 0, len);
291 	buf[0] = (type == IF_TYPE_MMC) ? (BIT(2) | BIT(0)) : BIT(0);
292 
293 	/* Set data xfer size */
294 	common->residue = common->data_size_from_cmnd = len;
295 
296 	return len;
297 }
298 
299 static void rkusb_fixup_cbwcb(struct fsg_common *common,
300 			      struct fsg_buffhd *bh)
301 {
302 	struct usb_request      *req = bh->outreq;
303 	struct fsg_bulk_cb_wrap *cbw = req->buf;
304 
305 	/* FIXME cbw.DataTransferLength was not set by Upgrade Tool */
306 	common->data_size = le32_to_cpu(cbw->DataTransferLength);
307 	if (common->data_size == 0) {
308 		common->data_size =
309 		get_unaligned_be16(&common->cmnd[7]) << 9;
310 		printf("Trasfer Length NOT set, please use new version tool\n");
311 		debug("%s %d, cmnd1 %x\n", __func__,
312 		      get_unaligned_be16(&common->cmnd[7]),
313 		      get_unaligned_be16(&common->cmnd[1]));
314 	}
315 	if (cbw->Flags & USB_BULK_IN_FLAG)
316 		common->data_dir = DATA_DIR_TO_HOST;
317 	else
318 		common->data_dir = DATA_DIR_FROM_HOST;
319 
320 	/* Not support */
321 	common->cmnd[1] = 0;
322 }
323 
324 static int rkusb_cmd_process(struct fsg_common *common,
325 			     struct fsg_buffhd *bh, int *reply)
326 {
327 	struct usb_request	*req = bh->outreq;
328 	struct fsg_bulk_cb_wrap	*cbw = req->buf;
329 	int rc;
330 
331 	dump_cbw(cbw);
332 
333 	if (rkusb_check_lun(common)) {
334 		*reply = -EINVAL;
335 		return RKUSB_RC_ERROR;
336 	}
337 
338 	switch (common->cmnd[0]) {
339 	case RKUSB_TEST_UNIT_READY:
340 		*reply = rkusb_do_test_unit_ready(common, bh);
341 		rc = RKUSB_RC_FINISHED;
342 		break;
343 
344 	case RKUSB_READ_FLASH_ID:
345 		*reply = rkusb_do_read_flash_id(common, bh);
346 		rc = RKUSB_RC_FINISHED;
347 		break;
348 
349 	case RKUSB_TEST_BAD_BLOCK:
350 		*reply = rkusb_do_test_bad_block(common, bh);
351 		rc = RKUSB_RC_FINISHED;
352 		break;
353 
354 	case RKUSB_LBA_READ_10:
355 		rkusb_fixup_cbwcb(common, bh);
356 		common->cmnd[0] = SC_READ_10;
357 		common->cmnd[1] = 0; /* Not support */
358 		rc = RKUSB_RC_CONTINUE;
359 		break;
360 
361 	case RKUSB_LBA_WRITE_10:
362 		rkusb_fixup_cbwcb(common, bh);
363 		common->cmnd[0] = SC_WRITE_10;
364 		common->cmnd[1] = 0; /* Not support */
365 		rc = RKUSB_RC_CONTINUE;
366 		break;
367 
368 	case RKUSB_READ_FLASH_INFO:
369 		*reply = rkusb_do_read_flash_info(common, bh);
370 		rc = RKUSB_RC_FINISHED;
371 		break;
372 
373 	case RKUSB_GET_CHIP_VER:
374 		*reply = rkusb_do_get_chip_info(common, bh);
375 		rc = RKUSB_RC_FINISHED;
376 		break;
377 
378 	case RKUSB_LBA_ERASE:
379 		*reply = rkusb_do_lba_erase(common, bh);
380 		rc = RKUSB_RC_FINISHED;
381 		break;
382 
383 	case RKUSB_READ_CAPACITY:
384 		*reply = rkusb_do_read_capacity(common, bh);
385 		rc = RKUSB_RC_FINISHED;
386 		break;
387 
388 	case RKUSB_RESET:
389 		*reply = rkusb_do_reset(common, bh);
390 		rc = RKUSB_RC_FINISHED;
391 		break;
392 
393 	case RKUSB_READ_10:
394 	case RKUSB_WRITE_10:
395 		printf("CMD Not support, pls use new version Tool\n");
396 	case RKUSB_SET_DEVICE_ID:
397 	case RKUSB_ERASE_10:
398 	case RKUSB_WRITE_SPARE:
399 	case RKUSB_READ_SPARE:
400 	case RKUSB_ERASE_10_FORCE:
401 	case RKUSB_GET_VERSION:
402 	case RKUSB_ERASE_SYS_DISK:
403 	case RKUSB_SDRAM_READ_10:
404 	case RKUSB_SDRAM_WRITE_10:
405 	case RKUSB_SDRAM_EXECUTE:
406 	case RKUSB_LOW_FORMAT:
407 	case RKUSB_SET_RESET_FLAG:
408 	case RKUSB_SPI_READ_10:
409 	case RKUSB_SPI_WRITE_10:
410 	case RKUSB_SESSION:
411 		/* Fall through */
412 	default:
413 		rc = RKUSB_RC_UNKNOWN_CMND;
414 		break;
415 	}
416 
417 	return rc;
418 }
419 
420 DECLARE_GADGET_BIND_CALLBACK(rkusb_ums_dnl, fsg_add);
421