xref: /rk3399_rockchip-uboot/drivers/usb/gadget/f_rockusb.c (revision 5c47e3d4734d329f41880f3b6ccf76f598763980)
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 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 	}
64 
65 	return 0;
66 }
67 
68 __maybe_unused
69 static inline void dump_cbw(struct fsg_bulk_cb_wrap *cbw)
70 {
71 	assert(!cbw);
72 
73 	debug("%s:\n", __func__);
74 	debug("Signature %x\n", cbw->Signature);
75 	debug("Tag %x\n", cbw->Tag);
76 	debug("DataTransferLength %x\n", cbw->DataTransferLength);
77 	debug("Flags %x\n", cbw->Flags);
78 	debug("LUN %x\n", cbw->Lun);
79 	debug("Length %x\n", cbw->Length);
80 	debug("OptionCode %x\n", cbw->CDB[0]);
81 	debug("SubCode %x\n", cbw->CDB[1]);
82 	debug("SectorAddr %x\n", get_unaligned_be32(&cbw->CDB[2]));
83 	debug("BlkSectors %x\n\n", get_unaligned_be16(&cbw->CDB[7]));
84 }
85 
86 static int rkusb_check_lun(struct fsg_common *common)
87 {
88 	struct fsg_lun *curlun;
89 
90 	/* Check the LUN */
91 	if (common->lun >= 0 && common->lun < common->nluns) {
92 		curlun = &common->luns[common->lun];
93 		if (common->cmnd[0] != SC_REQUEST_SENSE) {
94 			curlun->sense_data = SS_NO_SENSE;
95 			curlun->info_valid = 0;
96 		}
97 	} else {
98 		curlun = NULL;
99 		common->bad_lun_okay = 0;
100 
101 		/*
102 		 * INQUIRY and REQUEST SENSE commands are explicitly allowed
103 		 * to use unsupported LUNs; all others may not.
104 		 */
105 		if (common->cmnd[0] != SC_INQUIRY &&
106 		    common->cmnd[0] != SC_REQUEST_SENSE) {
107 			debug("unsupported LUN %d\n", common->lun);
108 			return -EINVAL;
109 		}
110 	}
111 
112 	return 0;
113 }
114 
115 static void __do_reset(struct usb_ep *ep, struct usb_request *req)
116 {
117 	u32 boot_flag = BOOT_NORMAL;
118 
119 	if (rkusb_rst_code == 0x03)
120 		boot_flag = BOOT_BROM_DOWNLOAD;
121 
122 	rkusb_rst_code = 0; /* restore to default */
123 	writel(boot_flag, (void *)CONFIG_ROCKCHIP_BOOT_MODE_REG);
124 
125 	do_reset(NULL, 0, 0, NULL);
126 }
127 
128 static int rkusb_do_reset(struct fsg_common *common,
129 			  struct fsg_buffhd *bh)
130 {
131 	common->data_size_from_cmnd = common->cmnd[4];
132 	common->residue = 0;
133 	bh->inreq->complete = __do_reset;
134 	bh->state = BUF_STATE_EMPTY;
135 
136 	rkusb_rst_code = !common->cmnd[1] ? 0xff : common->cmnd[1];
137 	return 0;
138 }
139 
140 static int rkusb_do_test_unit_ready(struct fsg_common *common,
141 				    struct fsg_buffhd *bh)
142 {
143 	common->residue = 0x06 << 24; /* Max block xfer support from host */
144 	common->data_dir = DATA_DIR_NONE;
145 	bh->state = BUF_STATE_EMPTY;
146 
147 	return 0;
148 }
149 
150 static int rkusb_do_read_flash_id(struct fsg_common *common,
151 				  struct fsg_buffhd *bh)
152 {
153 	u8 *buf = (u8 *)bh->buf;
154 	u32 len = common->data_size;
155 	enum if_type type = ums[common->lun].block_dev.if_type;
156 
157 	if (type == IF_TYPE_MMC)
158 		memcpy((void *)&buf[0], "EMMC ", 5);
159 	else if (type == IF_TYPE_RKNAND)
160 		memcpy((void *)&buf[0], "NAND ", 5);
161 	else
162 		memcpy((void *)&buf[0], "UNKN ", 5); /* unknown */
163 
164 	/* Set data xfer size */
165 	common->residue = common->data_size_from_cmnd = len;
166 
167 	return len;
168 }
169 
170 static int rkusb_do_test_bad_block(struct fsg_common *common,
171 				   struct fsg_buffhd *bh)
172 {
173 	u8 *buf = (u8 *)bh->buf;
174 	u32 len = common->data_size;
175 
176 	memset((void *)&buf[0], 0, len);
177 
178 	/* Set data xfer size */
179 	common->residue = common->data_size_from_cmnd = len;
180 
181 	return len;
182 }
183 
184 static int rkusb_do_read_flash_info(struct fsg_common *common,
185 				    struct fsg_buffhd *bh)
186 {
187 	u8 *buf = (u8 *)bh->buf;
188 	u32 len = common->data_size;
189 	struct rk_flash_info finfo = {
190 		.block_size = 1024,
191 		.ecc_bits = 0,
192 		.page_size = 4,
193 		.access_time = 40,
194 		.manufacturer = 0,
195 		.flash_mask = 0
196 	};
197 
198 	finfo.flash_size = (u32)ums[common->lun].block_dev.lba;
199 	if (finfo.flash_size)
200 		finfo.flash_mask = 1;
201 
202 	memset((void *)&buf[0], 0, len);
203 	memcpy((void *)&buf[0], (void *)&finfo, len);
204 
205 	/* Set data xfer size */
206 	common->residue = common->data_size_from_cmnd = len;
207 
208 	return len;
209 }
210 
211 static int rkusb_do_get_chip_info(struct fsg_common *common,
212 				  struct fsg_buffhd *bh)
213 {
214 	u8 *buf = (u8 *)bh->buf;
215 	u32 len = common->data_size;
216 	u32 chip_info[4];
217 
218 	memset((void *)chip_info, 0, sizeof(chip_info));
219 	rockchip_rockusb_get_chip_info(chip_info);
220 
221 	memset((void *)&buf[0], 0, len);
222 	memcpy((void *)&buf[0], (void *)chip_info, len);
223 
224 	/* Set data xfer size */
225 	common->residue = common->data_size_from_cmnd = len;
226 
227 	return len;
228 }
229 
230 static int rkusb_do_lba_erase(struct fsg_common *common,
231 			      struct fsg_buffhd *bh)
232 {
233 	struct fsg_lun *curlun = &common->luns[common->lun];
234 	u32 lba, amount;
235 	loff_t file_offset;
236 	int rc;
237 
238 	lba = get_unaligned_be32(&common->cmnd[2]);
239 	if (lba >= curlun->num_sectors) {
240 		curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
241 		rc = -EINVAL;
242 		goto out;
243 	}
244 
245 	file_offset = ((loff_t) lba) << 9;
246 	amount = get_unaligned_be16(&common->cmnd[7]) << 9;
247 	if (unlikely(amount == 0)) {
248 		curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
249 		rc = -EIO;
250 		goto out;
251 	}
252 
253 	/* Perform the erase */
254 	rc = ums[common->lun].erase_sector(&ums[common->lun],
255 			       file_offset / SECTOR_SIZE,
256 			       amount / SECTOR_SIZE);
257 	if (!rc) {
258 		curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
259 		rc = -EIO;
260 	}
261 
262 out:
263 	common->data_dir = DATA_DIR_NONE;
264 	bh->state = BUF_STATE_EMPTY;
265 
266 	return rc;
267 }
268 
269 static int rkusb_do_read_capacity(struct fsg_common *common,
270 				    struct fsg_buffhd *bh)
271 {
272 	u8 *buf = (u8 *)bh->buf;
273 	u32 len = common->data_size;
274 	enum if_type type = ums[common->lun].block_dev.if_type;
275 
276 	/*
277 	 * bit[0]: Direct LBA, 0: Disabled;
278 	 * bit[1]: Vendor Storage API, 0: default;
279 	 * bit[2]: First 4M Access, 0: Disabled;
280 	 * bit[3:63}: Reserved.
281 	 */
282 	memset((void *)&buf[0], 0, len);
283 	buf[0] = (type == IF_TYPE_MMC) ? (BIT(2) | BIT(0)) : BIT(0);
284 
285 	/* Set data xfer size */
286 	common->residue = common->data_size_from_cmnd = len;
287 
288 	return len;
289 }
290 
291 static int rkusb_cmd_process(struct fsg_common *common,
292 			     struct fsg_buffhd *bh, int *reply)
293 {
294 	struct usb_request	*req = bh->outreq;
295 	struct fsg_bulk_cb_wrap	*cbw = req->buf;
296 	int rc;
297 
298 	dump_cbw(cbw);
299 
300 	if (rkusb_check_lun(common)) {
301 		*reply = -EINVAL;
302 		return RKUSB_RC_ERROR;
303 	}
304 
305 	switch (common->cmnd[0]) {
306 	case RKUSB_TEST_UNIT_READY:
307 		*reply = rkusb_do_test_unit_ready(common, bh);
308 		rc = RKUSB_RC_FINISHED;
309 		break;
310 
311 	case RKUSB_READ_FLASH_ID:
312 		*reply = rkusb_do_read_flash_id(common, bh);
313 		rc = RKUSB_RC_FINISHED;
314 		break;
315 
316 	case RKUSB_TEST_BAD_BLOCK:
317 		*reply = rkusb_do_test_bad_block(common, bh);
318 		rc = RKUSB_RC_FINISHED;
319 		break;
320 
321 	case RKUSB_LBA_READ_10:
322 		common->cmnd[0] = SC_READ_10;
323 		common->cmnd[1] = 0; /* Not support */
324 		rc = RKUSB_RC_CONTINUE;
325 		break;
326 
327 	case RKUSB_LBA_WRITE_10:
328 		common->cmnd[0] = SC_WRITE_10;
329 		common->cmnd[1] = 0; /* Not support */
330 		rc = RKUSB_RC_CONTINUE;
331 		break;
332 
333 	case RKUSB_READ_FLASH_INFO:
334 		*reply = rkusb_do_read_flash_info(common, bh);
335 		rc = RKUSB_RC_FINISHED;
336 		break;
337 
338 	case RKUSB_GET_CHIP_VER:
339 		*reply = rkusb_do_get_chip_info(common, bh);
340 		rc = RKUSB_RC_FINISHED;
341 		break;
342 
343 	case RKUSB_LBA_ERASE:
344 		*reply = rkusb_do_lba_erase(common, bh);
345 		rc = RKUSB_RC_FINISHED;
346 		break;
347 
348 	case RKUSB_READ_CAPACITY:
349 		*reply = rkusb_do_read_capacity(common, bh);
350 		rc = RKUSB_RC_FINISHED;
351 		break;
352 
353 	case RKUSB_RESET:
354 		*reply = rkusb_do_reset(common, bh);
355 		rc = RKUSB_RC_FINISHED;
356 		break;
357 
358 	case RKUSB_SET_DEVICE_ID:
359 	case RKUSB_READ_10:
360 	case RKUSB_WRITE_10:
361 	case RKUSB_ERASE_10:
362 	case RKUSB_WRITE_SPARE:
363 	case RKUSB_READ_SPARE:
364 	case RKUSB_ERASE_10_FORCE:
365 	case RKUSB_GET_VERSION:
366 	case RKUSB_ERASE_SYS_DISK:
367 	case RKUSB_SDRAM_READ_10:
368 	case RKUSB_SDRAM_WRITE_10:
369 	case RKUSB_SDRAM_EXECUTE:
370 	case RKUSB_LOW_FORMAT:
371 	case RKUSB_SET_RESET_FLAG:
372 	case RKUSB_SPI_READ_10:
373 	case RKUSB_SPI_WRITE_10:
374 	case RKUSB_SESSION:
375 		/* Fall through */
376 	default:
377 		rc = RKUSB_RC_UNKNOWN_CMND;
378 		break;
379 	}
380 
381 	return rc;
382 }
383 
384 DECLARE_GADGET_BIND_CALLBACK(rkusb_ums_dnl, fsg_add);
385