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