xref: /openbmc/linux/drivers/usb/gadget/udc/bdc/bdc_cmd.c (revision 93d90ad7)
1 /*
2  * bdc_cmd.c - BRCM BDC USB3.0 device controller
3  *
4  * Copyright (C) 2014 Broadcom Corporation
5  *
6  * Author: Ashwini Pahuja
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2 of the License, or (at your
11  * option) any later version.
12  *
13  */
14 #include <linux/scatterlist.h>
15 #include <linux/slab.h>
16 
17 #include "bdc.h"
18 #include "bdc_cmd.h"
19 #include "bdc_dbg.h"
20 
21 /* Issues a cmd to cmd processor and waits for cmd completion */
22 static int bdc_issue_cmd(struct bdc *bdc, u32 cmd_sc, u32 param0,
23 							u32 param1, u32 param2)
24 {
25 	u32 timeout = BDC_CMD_TIMEOUT;
26 	u32 cmd_status;
27 	u32 temp;
28 
29 	bdc_writel(bdc->regs, BDC_CMDPAR0, param0);
30 	bdc_writel(bdc->regs, BDC_CMDPAR1, param1);
31 	bdc_writel(bdc->regs, BDC_CMDPAR2, param2);
32 
33 	/* Issue the cmd */
34 	/* Make sure the cmd params are written before asking HW to exec cmd */
35 	wmb();
36 	bdc_writel(bdc->regs, BDC_CMDSC, cmd_sc | BDC_CMD_CWS | BDC_CMD_SRD);
37 	do {
38 		temp = bdc_readl(bdc->regs, BDC_CMDSC);
39 		dev_dbg_ratelimited(bdc->dev, "cmdsc=%x", temp);
40 		cmd_status =  BDC_CMD_CST(temp);
41 		if (cmd_status != BDC_CMDS_BUSY)  {
42 			dev_dbg(bdc->dev,
43 				"command completed cmd_sts:%x\n", cmd_status);
44 			return cmd_status;
45 		}
46 		udelay(1);
47 	} while (timeout--);
48 
49 	dev_err(bdc->dev,
50 		"command operation timedout cmd_status=%d\n", cmd_status);
51 
52 	return cmd_status;
53 }
54 
55 /* Submits cmd and analyze the return value of bdc_issue_cmd */
56 static int bdc_submit_cmd(struct bdc *bdc, u32 cmd_sc,
57 					u32 param0, u32 param1,	u32 param2)
58 {
59 	u32 temp, cmd_status;
60 	int reset_bdc = 0;
61 	int ret;
62 
63 	temp = bdc_readl(bdc->regs, BDC_CMDSC);
64 	dev_dbg(bdc->dev,
65 		"%s:CMDSC:%08x cmdsc:%08x param0=%08x param1=%08x param2=%08x\n",
66 		 __func__, temp, cmd_sc, param0, param1, param2);
67 
68 	cmd_status = BDC_CMD_CST(temp);
69 	if (cmd_status  ==  BDC_CMDS_BUSY) {
70 		dev_err(bdc->dev, "command processor busy: %x\n", cmd_status);
71 		return -EBUSY;
72 	}
73 	ret = bdc_issue_cmd(bdc, cmd_sc, param0, param1, param2);
74 	switch (ret) {
75 	case BDC_CMDS_SUCC:
76 		dev_dbg(bdc->dev, "command completed successfully\n");
77 		ret = 0;
78 		break;
79 
80 	case BDC_CMDS_PARA:
81 		dev_err(bdc->dev, "command parameter error\n");
82 		ret = -EINVAL;
83 		break;
84 
85 	case BDC_CMDS_STAT:
86 		dev_err(bdc->dev, "Invalid device/ep state\n");
87 		ret = -EINVAL;
88 		break;
89 
90 	case BDC_CMDS_FAIL:
91 		dev_err(bdc->dev, "Command failed?\n");
92 		ret = -EAGAIN;
93 		break;
94 
95 	case BDC_CMDS_INTL:
96 		dev_err(bdc->dev, "BDC Internal error\n");
97 		reset_bdc = 1;
98 		ret = -ECONNRESET;
99 		break;
100 
101 	case BDC_CMDS_BUSY:
102 		dev_err(bdc->dev,
103 			"command timedout waited for %dusec\n",
104 			BDC_CMD_TIMEOUT);
105 		reset_bdc = 1;
106 		ret = -ECONNRESET;
107 		break;
108 	default:
109 		dev_dbg(bdc->dev, "Unknown command completion code:%x\n", ret);
110 	}
111 
112 	return ret;
113 }
114 
115 /* Deconfigure the endpoint from HW */
116 int bdc_dconfig_ep(struct bdc *bdc, struct bdc_ep *ep)
117 {
118 	u32 cmd_sc;
119 
120 	cmd_sc = BDC_SUB_CMD_DRP_EP|BDC_CMD_EPN(ep->ep_num)|BDC_CMD_EPC;
121 	dev_dbg(bdc->dev, "%s ep->ep_num =%d cmd_sc=%x\n", __func__,
122 							ep->ep_num, cmd_sc);
123 
124 	return bdc_submit_cmd(bdc, cmd_sc, 0, 0, 0);
125 }
126 
127 /* Reinitalize the bdlist after config ep command */
128 static void ep_bd_list_reinit(struct bdc_ep *ep)
129 {
130 	struct bdc *bdc = ep->bdc;
131 	struct bdc_bd *bd;
132 
133 	ep->bd_list.eqp_bdi = 0;
134 	ep->bd_list.hwd_bdi = 0;
135 	bd = ep->bd_list.bd_table_array[0]->start_bd;
136 	dev_dbg(bdc->dev, "%s ep:%p bd:%p\n", __func__, ep, bd);
137 	memset(bd, 0, sizeof(struct bdc_bd));
138 	bd->offset[3] |= cpu_to_le32(BD_SBF);
139 }
140 
141 /* Configure an endpoint */
142 int bdc_config_ep(struct bdc *bdc, struct bdc_ep *ep)
143 {
144 	const struct usb_ss_ep_comp_descriptor *comp_desc;
145 	const struct usb_endpoint_descriptor	*desc;
146 	u32 param0, param1, param2, cmd_sc;
147 	u32 mps, mbs, mul, si;
148 	int ret;
149 
150 	desc = ep->desc;
151 	comp_desc = ep->comp_desc;
152 	cmd_sc = mul = mbs = param2 = 0;
153 	param0 = lower_32_bits(ep->bd_list.bd_table_array[0]->dma);
154 	param1 = upper_32_bits(ep->bd_list.bd_table_array[0]->dma);
155 	cpu_to_le32s(&param0);
156 	cpu_to_le32s(&param1);
157 
158 	dev_dbg(bdc->dev, "%s: param0=%08x param1=%08x",
159 						__func__, param0, param1);
160 	si = desc->bInterval;
161 	si = clamp_val(si, 1, 16) - 1;
162 
163 	mps = usb_endpoint_maxp(desc);
164 	mps &= 0x7ff;
165 	param2 |= mps << MP_SHIFT;
166 	param2 |= usb_endpoint_type(desc) << EPT_SHIFT;
167 
168 	switch (bdc->gadget.speed) {
169 	case USB_SPEED_SUPER:
170 		if (usb_endpoint_xfer_int(desc) ||
171 					usb_endpoint_xfer_isoc(desc)) {
172 			param2 |= si;
173 			if (usb_endpoint_xfer_isoc(desc) && comp_desc)
174 					mul = comp_desc->bmAttributes;
175 
176 		}
177 		param2 |= mul << EPM_SHIFT;
178 		if (comp_desc)
179 			mbs = comp_desc->bMaxBurst;
180 		param2 |= mbs << MB_SHIFT;
181 		break;
182 
183 	case USB_SPEED_HIGH:
184 		if (usb_endpoint_xfer_isoc(desc) ||
185 					usb_endpoint_xfer_int(desc)) {
186 			param2 |= si;
187 
188 			mbs = (usb_endpoint_maxp(desc) & 0x1800) >> 11;
189 			param2 |= mbs << MB_SHIFT;
190 		}
191 		break;
192 
193 	case USB_SPEED_FULL:
194 	case USB_SPEED_LOW:
195 		/* the hardware accepts SI in 125usec range */
196 		if (usb_endpoint_xfer_isoc(desc))
197 			si += 3;
198 
199 		/*
200 		 * FS Int endpoints can have si of 1-255ms but the controller
201 		 * accepts 2^bInterval*125usec, so convert ms to nearest power
202 		 * of 2
203 		 */
204 		if (usb_endpoint_xfer_int(desc))
205 			si = fls(desc->bInterval * 8) - 1;
206 
207 		param2 |= si;
208 		break;
209 	default:
210 		dev_err(bdc->dev, "UNKNOWN speed ERR\n");
211 		return -EINVAL;
212 	}
213 
214 	cmd_sc |= BDC_CMD_EPC|BDC_CMD_EPN(ep->ep_num)|BDC_SUB_CMD_ADD_EP;
215 
216 	dev_dbg(bdc->dev, "cmd_sc=%x param2=%08x\n", cmd_sc, param2);
217 	ret = bdc_submit_cmd(bdc, cmd_sc, param0, param1, param2);
218 	if (ret) {
219 		dev_err(bdc->dev, "command failed :%x\n", ret);
220 		return ret;
221 	}
222 	ep_bd_list_reinit(ep);
223 
224 	return ret;
225 }
226 
227 /*
228  * Change the HW deq pointer, if this command is successful, HW will start
229  * fetching the next bd from address dma_addr.
230  */
231 int bdc_ep_bla(struct bdc *bdc, struct bdc_ep *ep, dma_addr_t dma_addr)
232 {
233 	u32 param0, param1;
234 	u32 cmd_sc = 0;
235 
236 	dev_dbg(bdc->dev, "%s: add=%08llx\n", __func__,
237 				(unsigned long long)(dma_addr));
238 	param0 = lower_32_bits(dma_addr);
239 	param1 = upper_32_bits(dma_addr);
240 	cpu_to_le32s(&param0);
241 	cpu_to_le32s(&param1);
242 
243 	cmd_sc |= BDC_CMD_EPN(ep->ep_num)|BDC_CMD_BLA;
244 	dev_dbg(bdc->dev, "cmd_sc=%x\n", cmd_sc);
245 
246 	return bdc_submit_cmd(bdc, cmd_sc, param0, param1, 0);
247 }
248 
249 /* Set the address sent bu Host in SET_ADD request */
250 int bdc_address_device(struct bdc *bdc, u32 add)
251 {
252 	u32 cmd_sc = 0;
253 	u32 param2;
254 
255 	dev_dbg(bdc->dev, "%s: add=%d\n", __func__, add);
256 	cmd_sc |=  BDC_SUB_CMD_ADD|BDC_CMD_DVC;
257 	param2 = add & 0x7f;
258 
259 	return bdc_submit_cmd(bdc, cmd_sc, 0, 0, param2);
260 }
261 
262 /* Send a Function Wake notification packet using FH command */
263 int bdc_function_wake_fh(struct bdc *bdc, u8 intf)
264 {
265 	u32 param0, param1;
266 	u32 cmd_sc = 0;
267 
268 	param0 = param1 = 0;
269 	dev_dbg(bdc->dev, "%s intf=%d\n", __func__, intf);
270 	cmd_sc  |=  BDC_CMD_FH;
271 	param0 |= TRA_PACKET;
272 	param0 |= (bdc->dev_addr << 25);
273 	param1 |= DEV_NOTF_TYPE;
274 	param1 |= (FWK_SUBTYPE<<4);
275 	dev_dbg(bdc->dev, "param0=%08x param1=%08x\n", param0, param1);
276 
277 	return bdc_submit_cmd(bdc, cmd_sc, param0, param1, 0);
278 }
279 
280 /* Send a Function Wake notification packet using DNC command */
281 int bdc_function_wake(struct bdc *bdc, u8 intf)
282 {
283 	u32 cmd_sc = 0;
284 	u32 param2 = 0;
285 
286 	dev_dbg(bdc->dev, "%s intf=%d", __func__, intf);
287 	param2 |= intf;
288 	cmd_sc |= BDC_SUB_CMD_FWK|BDC_CMD_DNC;
289 
290 	return bdc_submit_cmd(bdc, cmd_sc, 0, 0, param2);
291 }
292 
293 /* Stall the endpoint */
294 int bdc_ep_set_stall(struct bdc *bdc, int epnum)
295 {
296 	u32 cmd_sc = 0;
297 
298 	dev_dbg(bdc->dev, "%s epnum=%d\n", __func__, epnum);
299 	/* issue a stall endpoint command */
300 	cmd_sc |=  BDC_SUB_CMD_EP_STL | BDC_CMD_EPN(epnum) | BDC_CMD_EPO;
301 
302 	return bdc_submit_cmd(bdc, cmd_sc, 0, 0, 0);
303 }
304 
305 /* resets the endpoint, called when host sends CLEAR_FEATURE(HALT) */
306 int bdc_ep_clear_stall(struct bdc *bdc, int epnum)
307 {
308 	struct bdc_ep *ep;
309 	u32 cmd_sc = 0;
310 	int ret;
311 
312 	dev_dbg(bdc->dev, "%s: epnum=%d\n", __func__, epnum);
313 	ep = bdc->bdc_ep_array[epnum];
314 	/*
315 	 * If we are not in stalled then stall Endpoint and issue clear stall,
316 	 * his will reset the seq number for non EP0.
317 	 */
318 	if (epnum != 1) {
319 		/* if the endpoint it not stallled */
320 		if (!(ep->flags & BDC_EP_STALL)) {
321 			ret = bdc_ep_set_stall(bdc, epnum);
322 				if (ret)
323 					return ret;
324 		}
325 	}
326 	/* Preserve the seq number for ep0 only */
327 	if (epnum != 1)
328 		cmd_sc |= BDC_CMD_EPO_RST_SN;
329 
330 	/* issue a reset endpoint command */
331 	cmd_sc |=  BDC_SUB_CMD_EP_RST | BDC_CMD_EPN(epnum) | BDC_CMD_EPO;
332 
333 	ret = bdc_submit_cmd(bdc, cmd_sc, 0, 0, 0);
334 	if (ret) {
335 		dev_err(bdc->dev, "command failed:%x\n", ret);
336 		return ret;
337 	}
338 	bdc_notify_xfr(bdc, epnum);
339 
340 	return ret;
341 }
342 
343 /* Stop the endpoint, called when software wants to dequeue some request */
344 int bdc_stop_ep(struct bdc *bdc, int epnum)
345 {
346 	struct bdc_ep *ep;
347 	u32 cmd_sc = 0;
348 	int ret;
349 
350 	ep = bdc->bdc_ep_array[epnum];
351 	dev_dbg(bdc->dev, "%s: ep:%s ep->flags:%08x\n", __func__,
352 						ep->name, ep->flags);
353 	/* Endpoint has to be in running state to execute stop ep command */
354 	if (!(ep->flags & BDC_EP_ENABLED)) {
355 		dev_err(bdc->dev, "stop endpoint called for disabled ep\n");
356 		return   -EINVAL;
357 	}
358 	if ((ep->flags & BDC_EP_STALL) || (ep->flags & BDC_EP_STOP))
359 		return 0;
360 
361 	/* issue a stop endpoint command */
362 	cmd_sc |= BDC_CMD_EP0_XSD | BDC_SUB_CMD_EP_STP
363 				| BDC_CMD_EPN(epnum) | BDC_CMD_EPO;
364 
365 	ret = bdc_submit_cmd(bdc, cmd_sc, 0, 0, 0);
366 	if (ret) {
367 		dev_err(bdc->dev,
368 			"stop endpoint command didn't complete:%d ep:%s\n",
369 			ret, ep->name);
370 		return ret;
371 	}
372 	ep->flags |= BDC_EP_STOP;
373 	bdc_dump_epsts(bdc);
374 
375 	return ret;
376 }
377