root/drivers/usb/gadget/udc/bdc/bdc_cmd.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. bdc_issue_cmd
  2. bdc_submit_cmd
  3. bdc_dconfig_ep
  4. ep_bd_list_reinit
  5. bdc_config_ep
  6. bdc_ep_bla
  7. bdc_address_device
  8. bdc_function_wake_fh
  9. bdc_function_wake
  10. bdc_ep_set_stall
  11. bdc_ep_clear_stall
  12. bdc_stop_ep

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

/* [<][>][^][v][top][bottom][index][help] */