bdc_cmd.c 9.2 KB

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