xref: /rk3399_rockchip-uboot/drivers/usb/gadget/f_rockusb.c (revision 3016d3d41a52996a504f3cfdc50fd2438b97423e)
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 	enum if_type type = ums[common->lun].block_dev.if_type;
146 
147 	if (type == IF_TYPE_MMC)
148 		memcpy((void *)&buf[0], "EMMC ", 5);
149 	else if (type == IF_TYPE_RKNAND)
150 		memcpy((void *)&buf[0], "NAND ", 5);
151 	else
152 		memcpy((void *)&buf[0], "UNKN ", 5); /* unknown */
153 
154 	/* Set data xfer size */
155 	common->residue = common->data_size_from_cmnd = len;
156 
157 	return len;
158 }
159 
160 static int rkusb_do_test_bad_block(struct fsg_common *common,
161 				   struct fsg_buffhd *bh)
162 {
163 	u8 *buf = (u8 *)bh->buf;
164 	u32 len = common->data_size;
165 
166 	memset((void *)&buf[0], 0, len);
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_read_flash_info(struct fsg_common *common,
175 				    struct fsg_buffhd *bh)
176 {
177 	u8 *buf = (u8 *)bh->buf;
178 	u32 len = common->data_size;
179 	struct rk_flash_info finfo = {
180 		.block_size = 1024,
181 		.ecc_bits = 0,
182 		.page_size = 4,
183 		.access_time = 40,
184 		.manufacturer = 0,
185 		.flash_mask = 0
186 	};
187 
188 	finfo.flash_size = (u32)ums[common->lun].block_dev.lba;
189 	if (finfo.flash_size)
190 		finfo.flash_mask = 1;
191 
192 	memset((void *)&buf[0], 0, len);
193 	memcpy((void *)&buf[0], (void *)&finfo, len);
194 
195 	/* Set data xfer size */
196 	common->residue = common->data_size_from_cmnd = len;
197 
198 	return len;
199 }
200 
201 static int rkusb_do_lba_erase(struct fsg_common *common,
202 			      struct fsg_buffhd *bh)
203 {
204 	struct fsg_lun *curlun = &common->luns[common->lun];
205 	u32 lba, amount;
206 	loff_t file_offset;
207 	int rc;
208 
209 	lba = get_unaligned_be32(&common->cmnd[2]);
210 	if (lba >= curlun->num_sectors) {
211 		curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
212 		rc = -EINVAL;
213 		goto out;
214 	}
215 
216 	file_offset = ((loff_t) lba) << 9;
217 	amount = get_unaligned_be16(&common->cmnd[7]) << 9;
218 	if (unlikely(amount == 0)) {
219 		curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
220 		rc = -EIO;
221 		goto out;
222 	}
223 
224 	/* Perform the erase */
225 	rc = ums[common->lun].erase_sector(&ums[common->lun],
226 			       file_offset / SECTOR_SIZE,
227 			       amount / SECTOR_SIZE);
228 	if (!rc) {
229 		curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
230 		rc = -EIO;
231 	}
232 
233 out:
234 	common->data_dir = DATA_DIR_NONE;
235 	bh->state = BUF_STATE_EMPTY;
236 
237 	return rc;
238 }
239 
240 static int rkusb_do_read_capacity(struct fsg_common *common,
241 				    struct fsg_buffhd *bh)
242 {
243 	u8 *buf = (u8 *)bh->buf;
244 	u32 len = common->data_size;
245 	enum if_type type = ums[common->lun].block_dev.if_type;
246 
247 	/*
248 	 * bit[0]: Direct LBA, 0: Disabled;
249 	 * bit[1]: Vendor Storage API, 0: default;
250 	 * bit[2]: First 4M Access, 0: Disabled;
251 	 * bit[3:63}: Reserved.
252 	 */
253 	memset((void *)&buf[0], 0, len);
254 	buf[0] = (type == IF_TYPE_MMC) ? (BIT(2) | BIT(0)) : BIT(0);
255 
256 	/* Set data xfer size */
257 	common->residue = common->data_size_from_cmnd = len;
258 
259 	return len;
260 }
261 
262 static int rkusb_cmd_process(struct fsg_common *common,
263 			     struct fsg_buffhd *bh, int *reply)
264 {
265 	struct usb_request	*req = bh->outreq;
266 	struct fsg_bulk_cb_wrap	*cbw = req->buf;
267 	int rc;
268 
269 	dump_cbw(cbw);
270 
271 	if (rkusb_check_lun(common)) {
272 		*reply = -EINVAL;
273 		return RKUSB_RC_ERROR;
274 	}
275 
276 	switch (common->cmnd[0]) {
277 	case RKUSB_TEST_UNIT_READY:
278 		*reply = rkusb_do_test_unit_ready(common, bh);
279 		rc = RKUSB_RC_FINISHED;
280 		break;
281 
282 	case RKUSB_READ_FLASH_ID:
283 		*reply = rkusb_do_read_flash_id(common, bh);
284 		rc = RKUSB_RC_FINISHED;
285 		break;
286 
287 	case RKUSB_TEST_BAD_BLOCK:
288 		*reply = rkusb_do_test_bad_block(common, bh);
289 		rc = RKUSB_RC_FINISHED;
290 		break;
291 
292 	case RKUSB_LBA_READ_10:
293 		common->cmnd[0] = SC_READ_10;
294 		common->cmnd[1] = 0; /* Not support */
295 		rc = RKUSB_RC_CONTINUE;
296 		break;
297 
298 	case RKUSB_LBA_WRITE_10:
299 		common->cmnd[0] = SC_WRITE_10;
300 		common->cmnd[1] = 0; /* Not support */
301 		rc = RKUSB_RC_CONTINUE;
302 		break;
303 
304 	case RKUSB_READ_FLASH_INFO:
305 		*reply = rkusb_do_read_flash_info(common, bh);
306 		rc = RKUSB_RC_FINISHED;
307 		break;
308 
309 	case RKUSB_LBA_ERASE:
310 		*reply = rkusb_do_lba_erase(common, bh);
311 		rc = RKUSB_RC_FINISHED;
312 		break;
313 
314 	case RKUSB_READ_CAPACITY:
315 		*reply = rkusb_do_read_capacity(common, bh);
316 		rc = RKUSB_RC_FINISHED;
317 		break;
318 
319 	case RKUSB_RESET:
320 		*reply = rkusb_do_reset(common, bh);
321 		rc = RKUSB_RC_FINISHED;
322 		break;
323 
324 	case RKUSB_SET_DEVICE_ID:
325 	case RKUSB_READ_10:
326 	case RKUSB_WRITE_10:
327 	case RKUSB_ERASE_10:
328 	case RKUSB_WRITE_SPARE:
329 	case RKUSB_READ_SPARE:
330 	case RKUSB_ERASE_10_FORCE:
331 	case RKUSB_GET_VERSION:
332 	case RKUSB_ERASE_SYS_DISK:
333 	case RKUSB_SDRAM_READ_10:
334 	case RKUSB_SDRAM_WRITE_10:
335 	case RKUSB_SDRAM_EXECUTE:
336 	case RKUSB_GET_CHIP_VER:
337 	case RKUSB_LOW_FORMAT:
338 	case RKUSB_SET_RESET_FLAG:
339 	case RKUSB_SPI_READ_10:
340 	case RKUSB_SPI_WRITE_10:
341 	case RKUSB_SESSION:
342 		/* Fall through */
343 	default:
344 		rc = RKUSB_RC_UNKNOWN_CMND;
345 		break;
346 	}
347 
348 	return rc;
349 }
350 
351 DECLARE_GADGET_BIND_CALLBACK(rkusb_ums_dnl, fsg_add);
352