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