USB: add SPDX identifiers to all remaining files in drivers/usb/
[linux-2.6-block.git] / drivers / usb / gadget / udc / bdc / bdc_cmd.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * bdc_cmd.c - BRCM BDC USB3.0 device controller
4  *
5  * Copyright (C) 2014 Broadcom Corporation
6  *
7  * Author: Ashwini Pahuja
8  *
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.
13  *
14  */
15 #include <linux/scatterlist.h>
16 #include <linux/slab.h>
17
18 #include "bdc.h"
19 #include "bdc_cmd.h"
20 #include "bdc_dbg.h"
21
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)
25 {
26         u32 timeout = BDC_CMD_TIMEOUT;
27         u32 cmd_status;
28         u32 temp;
29
30         bdc_writel(bdc->regs, BDC_CMDPAR0, param0);
31         bdc_writel(bdc->regs, BDC_CMDPAR1, param1);
32         bdc_writel(bdc->regs, BDC_CMDPAR2, param2);
33
34         /* Issue the cmd */
35         /* Make sure the cmd params are written before asking HW to exec cmd */
36         wmb();
37         bdc_writel(bdc->regs, BDC_CMDSC, cmd_sc | BDC_CMD_CWS | BDC_CMD_SRD);
38         do {
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)  {
43                         dev_dbg(bdc->dev,
44                                 "command completed cmd_sts:%x\n", cmd_status);
45                         return cmd_status;
46                 }
47                 udelay(1);
48         } while (timeout--);
49
50         dev_err(bdc->dev,
51                 "command operation timedout cmd_status=%d\n", cmd_status);
52
53         return cmd_status;
54 }
55
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)
59 {
60         u32 temp, cmd_status;
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                 ret = -ECONNRESET;
98                 break;
99
100         case BDC_CMDS_BUSY:
101                 dev_err(bdc->dev,
102                         "command timedout waited for %dusec\n",
103                         BDC_CMD_TIMEOUT);
104                 ret = -ECONNRESET;
105                 break;
106         default:
107                 dev_dbg(bdc->dev, "Unknown command completion code:%x\n", ret);
108         }
109
110         return ret;
111 }
112
113 /* Deconfigure the endpoint from HW */
114 int bdc_dconfig_ep(struct bdc *bdc, struct bdc_ep *ep)
115 {
116         u32 cmd_sc;
117
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__,
120                                                         ep->ep_num, cmd_sc);
121
122         return bdc_submit_cmd(bdc, cmd_sc, 0, 0, 0);
123 }
124
125 /* Reinitalize the bdlist after config ep command */
126 static void ep_bd_list_reinit(struct bdc_ep *ep)
127 {
128         struct bdc *bdc = ep->bdc;
129         struct bdc_bd *bd;
130
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);
137 }
138
139 /* Configure an endpoint */
140 int bdc_config_ep(struct bdc *bdc, struct bdc_ep *ep)
141 {
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;
146         int ret;
147
148         desc = ep->desc;
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(&param0);
154         cpu_to_le32s(&param1);
155
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;
160
161         mps = usb_endpoint_maxp(desc);
162         mps &= 0x7ff;
163         param2 |= mps << MP_SHIFT;
164         param2 |= usb_endpoint_type(desc) << EPT_SHIFT;
165
166         switch (bdc->gadget.speed) {
167         case USB_SPEED_SUPER:
168                 if (usb_endpoint_xfer_int(desc) ||
169                                         usb_endpoint_xfer_isoc(desc)) {
170                         param2 |= si;
171                         if (usb_endpoint_xfer_isoc(desc) && comp_desc)
172                                         mul = comp_desc->bmAttributes;
173
174                 }
175                 param2 |= mul << EPM_SHIFT;
176                 if (comp_desc)
177                         mbs = comp_desc->bMaxBurst;
178                 param2 |= mbs << MB_SHIFT;
179                 break;
180
181         case USB_SPEED_HIGH:
182                 if (usb_endpoint_xfer_isoc(desc) ||
183                                         usb_endpoint_xfer_int(desc)) {
184                         param2 |= si;
185
186                         mbs = usb_endpoint_maxp_mult(desc);
187                         param2 |= mbs << MB_SHIFT;
188                 }
189                 break;
190
191         case USB_SPEED_FULL:
192         case USB_SPEED_LOW:
193                 /* the hardware accepts SI in 125usec range */
194                 if (usb_endpoint_xfer_isoc(desc))
195                         si += 3;
196
197                 /*
198                  * FS Int endpoints can have si of 1-255ms but the controller
199                  * accepts 2^bInterval*125usec, so convert ms to nearest power
200                  * of 2
201                  */
202                 if (usb_endpoint_xfer_int(desc))
203                         si = fls(desc->bInterval * 8) - 1;
204
205                 param2 |= si;
206                 break;
207         default:
208                 dev_err(bdc->dev, "UNKNOWN speed ERR\n");
209                 return -EINVAL;
210         }
211
212         cmd_sc |= BDC_CMD_EPC|BDC_CMD_EPN(ep->ep_num)|BDC_SUB_CMD_ADD_EP;
213
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);
216         if (ret) {
217                 dev_err(bdc->dev, "command failed :%x\n", ret);
218                 return ret;
219         }
220         ep_bd_list_reinit(ep);
221
222         return ret;
223 }
224
225 /*
226  * Change the HW deq pointer, if this command is successful, HW will start
227  * fetching the next bd from address dma_addr.
228  */
229 int bdc_ep_bla(struct bdc *bdc, struct bdc_ep *ep, dma_addr_t dma_addr)
230 {
231         u32 param0, param1;
232         u32 cmd_sc = 0;
233
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(&param0);
239         cpu_to_le32s(&param1);
240
241         cmd_sc |= BDC_CMD_EPN(ep->ep_num)|BDC_CMD_BLA;
242         dev_dbg(bdc->dev, "cmd_sc=%x\n", cmd_sc);
243
244         return bdc_submit_cmd(bdc, cmd_sc, param0, param1, 0);
245 }
246
247 /* Set the address sent bu Host in SET_ADD request */
248 int bdc_address_device(struct bdc *bdc, u32 add)
249 {
250         u32 cmd_sc = 0;
251         u32 param2;
252
253         dev_dbg(bdc->dev, "%s: add=%d\n", __func__, add);
254         cmd_sc |=  BDC_SUB_CMD_ADD|BDC_CMD_DVC;
255         param2 = add & 0x7f;
256
257         return bdc_submit_cmd(bdc, cmd_sc, 0, 0, param2);
258 }
259
260 /* Send a Function Wake notification packet using FH command */
261 int bdc_function_wake_fh(struct bdc *bdc, u8 intf)
262 {
263         u32 param0, param1;
264         u32 cmd_sc = 0;
265
266         param0 = param1 = 0;
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);
274
275         return bdc_submit_cmd(bdc, cmd_sc, param0, param1, 0);
276 }
277
278 /* Send a Function Wake notification packet using DNC command */
279 int bdc_function_wake(struct bdc *bdc, u8 intf)
280 {
281         u32 cmd_sc = 0;
282         u32 param2 = 0;
283
284         dev_dbg(bdc->dev, "%s intf=%d", __func__, intf);
285         param2 |= intf;
286         cmd_sc |= BDC_SUB_CMD_FWK|BDC_CMD_DNC;
287
288         return bdc_submit_cmd(bdc, cmd_sc, 0, 0, param2);
289 }
290
291 /* Stall the endpoint */
292 int bdc_ep_set_stall(struct bdc *bdc, int epnum)
293 {
294         u32 cmd_sc = 0;
295
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;
299
300         return bdc_submit_cmd(bdc, cmd_sc, 0, 0, 0);
301 }
302
303 /* resets the endpoint, called when host sends CLEAR_FEATURE(HALT) */
304 int bdc_ep_clear_stall(struct bdc *bdc, int epnum)
305 {
306         struct bdc_ep *ep;
307         u32 cmd_sc = 0;
308         int ret;
309
310         dev_dbg(bdc->dev, "%s: epnum=%d\n", __func__, epnum);
311         ep = bdc->bdc_ep_array[epnum];
312         /*
313          * If we are not in stalled then stall Endpoint and issue clear stall,
314          * his will reset the seq number for non EP0.
315          */
316         if (epnum != 1) {
317                 /* if the endpoint it not stallled */
318                 if (!(ep->flags & BDC_EP_STALL)) {
319                         ret = bdc_ep_set_stall(bdc, epnum);
320                                 if (ret)
321                                         return ret;
322                 }
323         }
324         /* Preserve the seq number for ep0 only */
325         if (epnum != 1)
326                 cmd_sc |= BDC_CMD_EPO_RST_SN;
327
328         /* issue a reset endpoint command */
329         cmd_sc |=  BDC_SUB_CMD_EP_RST | BDC_CMD_EPN(epnum) | BDC_CMD_EPO;
330
331         ret = bdc_submit_cmd(bdc, cmd_sc, 0, 0, 0);
332         if (ret) {
333                 dev_err(bdc->dev, "command failed:%x\n", ret);
334                 return ret;
335         }
336         bdc_notify_xfr(bdc, epnum);
337
338         return ret;
339 }
340
341 /* Stop the endpoint, called when software wants to dequeue some request */
342 int bdc_stop_ep(struct bdc *bdc, int epnum)
343 {
344         struct bdc_ep *ep;
345         u32 cmd_sc = 0;
346         int ret;
347
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");
354                 return   -EINVAL;
355         }
356         if ((ep->flags & BDC_EP_STALL) || (ep->flags & BDC_EP_STOP))
357                 return 0;
358
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;
362
363         ret = bdc_submit_cmd(bdc, cmd_sc, 0, 0, 0);
364         if (ret) {
365                 dev_err(bdc->dev,
366                         "stop endpoint command didn't complete:%d ep:%s\n",
367                         ret, ep->name);
368                 return ret;
369         }
370         ep->flags |= BDC_EP_STOP;
371         bdc_dump_epsts(bdc);
372
373         return ret;
374 }