bdc_cmd.c 9.3 KB

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