bdc_cmd.c 9.0 KB

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