1 // SPDX-License-Identifier: GPL-2.0+
3 * bdc_cmd.c - BRCM BDC USB3.0 device controller
5 * Copyright (C) 2014 Broadcom Corporation
7 * Author: Ashwini Pahuja
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
15 #include <linux/scatterlist.h>
16 #include <linux/slab.h>
22 /* Issues a cmd to cmd processor and waits for cmd completion */
23 static int bdc_issue_cmd(struct bdc *bdc, u32 cmd_sc, u32 param0,
24 u32 param1, u32 param2)
26 u32 timeout = BDC_CMD_TIMEOUT;
30 bdc_writel(bdc->regs, BDC_CMDPAR0, param0);
31 bdc_writel(bdc->regs, BDC_CMDPAR1, param1);
32 bdc_writel(bdc->regs, BDC_CMDPAR2, param2);
35 /* Make sure the cmd params are written before asking HW to exec cmd */
37 bdc_writel(bdc->regs, BDC_CMDSC, cmd_sc | BDC_CMD_CWS | BDC_CMD_SRD);
39 temp = bdc_readl(bdc->regs, BDC_CMDSC);
40 dev_dbg_ratelimited(bdc->dev, "cmdsc=%x", temp);
41 cmd_status = BDC_CMD_CST(temp);
42 if (cmd_status != BDC_CMDS_BUSY) {
44 "command completed cmd_sts:%x\n", cmd_status);
51 "command operation timedout cmd_status=%d\n", cmd_status);
56 /* Submits cmd and analyze the return value of bdc_issue_cmd */
57 static int bdc_submit_cmd(struct bdc *bdc, u32 cmd_sc,
58 u32 param0, u32 param1, u32 param2)
63 temp = bdc_readl(bdc->regs, BDC_CMDSC);
65 "%s:CMDSC:%08x cmdsc:%08x param0=%08x param1=%08x param2=%08x\n",
66 __func__, temp, cmd_sc, param0, param1, param2);
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);
73 ret = bdc_issue_cmd(bdc, cmd_sc, param0, param1, param2);
76 dev_dbg(bdc->dev, "command completed successfully\n");
81 dev_err(bdc->dev, "command parameter error\n");
86 dev_err(bdc->dev, "Invalid device/ep state\n");
91 dev_err(bdc->dev, "Command failed?\n");
96 dev_err(bdc->dev, "BDC Internal error\n");
102 "command timedout waited for %dusec\n",
107 dev_dbg(bdc->dev, "Unknown command completion code:%x\n", ret);
113 /* Deconfigure the endpoint from HW */
114 int bdc_dconfig_ep(struct bdc *bdc, struct bdc_ep *ep)
118 cmd_sc = BDC_SUB_CMD_DRP_EP|BDC_CMD_EPN(ep->ep_num)|BDC_CMD_EPC;
119 dev_dbg(bdc->dev, "%s ep->ep_num =%d cmd_sc=%x\n", __func__,
122 return bdc_submit_cmd(bdc, cmd_sc, 0, 0, 0);
125 /* Reinitalize the bdlist after config ep command */
126 static void ep_bd_list_reinit(struct bdc_ep *ep)
128 struct bdc *bdc = ep->bdc;
131 ep->bd_list.eqp_bdi = 0;
132 ep->bd_list.hwd_bdi = 0;
133 bd = ep->bd_list.bd_table_array[0]->start_bd;
134 dev_dbg(bdc->dev, "%s ep:%p bd:%p\n", __func__, ep, bd);
135 memset(bd, 0, sizeof(struct bdc_bd));
136 bd->offset[3] |= cpu_to_le32(BD_SBF);
139 /* Configure an endpoint */
140 int bdc_config_ep(struct bdc *bdc, struct bdc_ep *ep)
142 const struct usb_ss_ep_comp_descriptor *comp_desc;
143 const struct usb_endpoint_descriptor *desc;
144 u32 param0, param1, param2, cmd_sc;
145 u32 mps, mbs, mul, si;
149 comp_desc = ep->comp_desc;
150 cmd_sc = mul = mbs = param2 = 0;
151 param0 = lower_32_bits(ep->bd_list.bd_table_array[0]->dma);
152 param1 = upper_32_bits(ep->bd_list.bd_table_array[0]->dma);
153 cpu_to_le32s(¶m0);
154 cpu_to_le32s(¶m1);
156 dev_dbg(bdc->dev, "%s: param0=%08x param1=%08x",
157 __func__, param0, param1);
158 si = desc->bInterval;
159 si = clamp_val(si, 1, 16) - 1;
161 mps = usb_endpoint_maxp(desc);
163 param2 |= mps << MP_SHIFT;
164 param2 |= usb_endpoint_type(desc) << EPT_SHIFT;
166 switch (bdc->gadget.speed) {
167 case USB_SPEED_SUPER:
168 if (usb_endpoint_xfer_int(desc) ||
169 usb_endpoint_xfer_isoc(desc)) {
171 if (usb_endpoint_xfer_isoc(desc) && comp_desc)
172 mul = comp_desc->bmAttributes;
175 param2 |= mul << EPM_SHIFT;
177 mbs = comp_desc->bMaxBurst;
178 param2 |= mbs << MB_SHIFT;
182 if (usb_endpoint_xfer_isoc(desc) ||
183 usb_endpoint_xfer_int(desc)) {
186 mbs = usb_endpoint_maxp_mult(desc);
187 param2 |= mbs << MB_SHIFT;
193 /* the hardware accepts SI in 125usec range */
194 if (usb_endpoint_xfer_isoc(desc))
198 * FS Int endpoints can have si of 1-255ms but the controller
199 * accepts 2^bInterval*125usec, so convert ms to nearest power
202 if (usb_endpoint_xfer_int(desc))
203 si = fls(desc->bInterval * 8) - 1;
208 dev_err(bdc->dev, "UNKNOWN speed ERR\n");
212 cmd_sc |= BDC_CMD_EPC|BDC_CMD_EPN(ep->ep_num)|BDC_SUB_CMD_ADD_EP;
214 dev_dbg(bdc->dev, "cmd_sc=%x param2=%08x\n", cmd_sc, param2);
215 ret = bdc_submit_cmd(bdc, cmd_sc, param0, param1, param2);
217 dev_err(bdc->dev, "command failed :%x\n", ret);
220 ep_bd_list_reinit(ep);
226 * Change the HW deq pointer, if this command is successful, HW will start
227 * fetching the next bd from address dma_addr.
229 int bdc_ep_bla(struct bdc *bdc, struct bdc_ep *ep, dma_addr_t dma_addr)
234 dev_dbg(bdc->dev, "%s: add=%08llx\n", __func__,
235 (unsigned long long)(dma_addr));
236 param0 = lower_32_bits(dma_addr);
237 param1 = upper_32_bits(dma_addr);
238 cpu_to_le32s(¶m0);
239 cpu_to_le32s(¶m1);
241 cmd_sc |= BDC_CMD_EPN(ep->ep_num)|BDC_CMD_BLA;
242 dev_dbg(bdc->dev, "cmd_sc=%x\n", cmd_sc);
244 return bdc_submit_cmd(bdc, cmd_sc, param0, param1, 0);
247 /* Set the address sent bu Host in SET_ADD request */
248 int bdc_address_device(struct bdc *bdc, u32 add)
253 dev_dbg(bdc->dev, "%s: add=%d\n", __func__, add);
254 cmd_sc |= BDC_SUB_CMD_ADD|BDC_CMD_DVC;
257 return bdc_submit_cmd(bdc, cmd_sc, 0, 0, param2);
260 /* Send a Function Wake notification packet using FH command */
261 int bdc_function_wake_fh(struct bdc *bdc, u8 intf)
267 dev_dbg(bdc->dev, "%s intf=%d\n", __func__, intf);
268 cmd_sc |= BDC_CMD_FH;
269 param0 |= TRA_PACKET;
270 param0 |= (bdc->dev_addr << 25);
271 param1 |= DEV_NOTF_TYPE;
272 param1 |= (FWK_SUBTYPE<<4);
273 dev_dbg(bdc->dev, "param0=%08x param1=%08x\n", param0, param1);
275 return bdc_submit_cmd(bdc, cmd_sc, param0, param1, 0);
278 /* Send a Function Wake notification packet using DNC command */
279 int bdc_function_wake(struct bdc *bdc, u8 intf)
284 dev_dbg(bdc->dev, "%s intf=%d", __func__, intf);
286 cmd_sc |= BDC_SUB_CMD_FWK|BDC_CMD_DNC;
288 return bdc_submit_cmd(bdc, cmd_sc, 0, 0, param2);
291 /* Stall the endpoint */
292 int bdc_ep_set_stall(struct bdc *bdc, int epnum)
296 dev_dbg(bdc->dev, "%s epnum=%d\n", __func__, epnum);
297 /* issue a stall endpoint command */
298 cmd_sc |= BDC_SUB_CMD_EP_STL | BDC_CMD_EPN(epnum) | BDC_CMD_EPO;
300 return bdc_submit_cmd(bdc, cmd_sc, 0, 0, 0);
303 /* resets the endpoint, called when host sends CLEAR_FEATURE(HALT) */
304 int bdc_ep_clear_stall(struct bdc *bdc, int epnum)
310 dev_dbg(bdc->dev, "%s: epnum=%d\n", __func__, epnum);
311 ep = bdc->bdc_ep_array[epnum];
313 * If we are not in stalled then stall Endpoint and issue clear stall,
314 * his will reset the seq number for non EP0.
317 /* if the endpoint it not stallled */
318 if (!(ep->flags & BDC_EP_STALL)) {
319 ret = bdc_ep_set_stall(bdc, epnum);
324 /* Preserve the seq number for ep0 only */
326 cmd_sc |= BDC_CMD_EPO_RST_SN;
328 /* issue a reset endpoint command */
329 cmd_sc |= BDC_SUB_CMD_EP_RST | BDC_CMD_EPN(epnum) | BDC_CMD_EPO;
331 ret = bdc_submit_cmd(bdc, cmd_sc, 0, 0, 0);
333 dev_err(bdc->dev, "command failed:%x\n", ret);
336 bdc_notify_xfr(bdc, epnum);
341 /* Stop the endpoint, called when software wants to dequeue some request */
342 int bdc_stop_ep(struct bdc *bdc, int epnum)
348 ep = bdc->bdc_ep_array[epnum];
349 dev_dbg(bdc->dev, "%s: ep:%s ep->flags:%08x\n", __func__,
350 ep->name, ep->flags);
351 /* Endpoint has to be in running state to execute stop ep command */
352 if (!(ep->flags & BDC_EP_ENABLED)) {
353 dev_err(bdc->dev, "stop endpoint called for disabled ep\n");
356 if ((ep->flags & BDC_EP_STALL) || (ep->flags & BDC_EP_STOP))
359 /* issue a stop endpoint command */
360 cmd_sc |= BDC_CMD_EP0_XSD | BDC_SUB_CMD_EP_STP
361 | BDC_CMD_EPN(epnum) | BDC_CMD_EPO;
363 ret = bdc_submit_cmd(bdc, cmd_sc, 0, 0, 0);
366 "stop endpoint command didn't complete:%d ep:%s\n",
370 ep->flags |= BDC_EP_STOP;