core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * aspeed-vhub -- Driver for Aspeed SoC "vHub" USB gadget
  4. *
  5. * core.c - Top level support
  6. *
  7. * Copyright 2017 IBM Corporation
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/delay.h>
  18. #include <linux/ioport.h>
  19. #include <linux/slab.h>
  20. #include <linux/errno.h>
  21. #include <linux/list.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/proc_fs.h>
  24. #include <linux/prefetch.h>
  25. #include <linux/clk.h>
  26. #include <linux/usb/gadget.h>
  27. #include <linux/of.h>
  28. #include <linux/of_gpio.h>
  29. #include <linux/regmap.h>
  30. #include <linux/dma-mapping.h>
  31. #include "vhub.h"
  32. void ast_vhub_done(struct ast_vhub_ep *ep, struct ast_vhub_req *req,
  33. int status)
  34. {
  35. bool internal = req->internal;
  36. EPVDBG(ep, "completing request @%p, status %d\n", req, status);
  37. list_del_init(&req->queue);
  38. if (req->req.status == -EINPROGRESS)
  39. req->req.status = status;
  40. if (req->req.dma) {
  41. if (!WARN_ON(!ep->dev))
  42. usb_gadget_unmap_request(&ep->dev->gadget,
  43. &req->req, ep->epn.is_in);
  44. req->req.dma = 0;
  45. }
  46. /*
  47. * If this isn't an internal EP0 request, call the core
  48. * to call the gadget completion.
  49. */
  50. if (!internal) {
  51. spin_unlock(&ep->vhub->lock);
  52. usb_gadget_giveback_request(&ep->ep, &req->req);
  53. spin_lock(&ep->vhub->lock);
  54. }
  55. }
  56. void ast_vhub_nuke(struct ast_vhub_ep *ep, int status)
  57. {
  58. struct ast_vhub_req *req;
  59. EPDBG(ep, "Nuking\n");
  60. /* Beware, lock will be dropped & req-acquired by done() */
  61. while (!list_empty(&ep->queue)) {
  62. req = list_first_entry(&ep->queue, struct ast_vhub_req, queue);
  63. ast_vhub_done(ep, req, status);
  64. }
  65. }
  66. struct usb_request *ast_vhub_alloc_request(struct usb_ep *u_ep,
  67. gfp_t gfp_flags)
  68. {
  69. struct ast_vhub_req *req;
  70. req = kzalloc(sizeof(*req), gfp_flags);
  71. if (!req)
  72. return NULL;
  73. return &req->req;
  74. }
  75. void ast_vhub_free_request(struct usb_ep *u_ep, struct usb_request *u_req)
  76. {
  77. struct ast_vhub_req *req = to_ast_req(u_req);
  78. kfree(req);
  79. }
  80. static irqreturn_t ast_vhub_irq(int irq, void *data)
  81. {
  82. struct ast_vhub *vhub = data;
  83. irqreturn_t iret = IRQ_NONE;
  84. u32 istat;
  85. /* Stale interrupt while tearing down */
  86. if (!vhub->ep0_bufs)
  87. return IRQ_NONE;
  88. spin_lock(&vhub->lock);
  89. /* Read and ACK interrupts */
  90. istat = readl(vhub->regs + AST_VHUB_ISR);
  91. if (!istat)
  92. goto bail;
  93. writel(istat, vhub->regs + AST_VHUB_ISR);
  94. iret = IRQ_HANDLED;
  95. UDCVDBG(vhub, "irq status=%08x, ep_acks=%08x ep_nacks=%08x\n",
  96. istat,
  97. readl(vhub->regs + AST_VHUB_EP_ACK_ISR),
  98. readl(vhub->regs + AST_VHUB_EP_NACK_ISR));
  99. /* Handle generic EPs first */
  100. if (istat & VHUB_IRQ_EP_POOL_ACK_STALL) {
  101. u32 i, ep_acks = readl(vhub->regs + AST_VHUB_EP_ACK_ISR);
  102. writel(ep_acks, vhub->regs + AST_VHUB_EP_ACK_ISR);
  103. for (i = 0; ep_acks && i < AST_VHUB_NUM_GEN_EPs; i++) {
  104. u32 mask = VHUB_EP_IRQ(i);
  105. if (ep_acks & mask) {
  106. ast_vhub_epn_ack_irq(&vhub->epns[i]);
  107. ep_acks &= ~mask;
  108. }
  109. }
  110. }
  111. /* Handle device interrupts */
  112. if (istat & (VHUB_IRQ_DEVICE1 |
  113. VHUB_IRQ_DEVICE2 |
  114. VHUB_IRQ_DEVICE3 |
  115. VHUB_IRQ_DEVICE4 |
  116. VHUB_IRQ_DEVICE5)) {
  117. if (istat & VHUB_IRQ_DEVICE1)
  118. ast_vhub_dev_irq(&vhub->ports[0].dev);
  119. if (istat & VHUB_IRQ_DEVICE2)
  120. ast_vhub_dev_irq(&vhub->ports[1].dev);
  121. if (istat & VHUB_IRQ_DEVICE3)
  122. ast_vhub_dev_irq(&vhub->ports[2].dev);
  123. if (istat & VHUB_IRQ_DEVICE4)
  124. ast_vhub_dev_irq(&vhub->ports[3].dev);
  125. if (istat & VHUB_IRQ_DEVICE5)
  126. ast_vhub_dev_irq(&vhub->ports[4].dev);
  127. }
  128. /* Handle top-level vHub EP0 interrupts */
  129. if (istat & (VHUB_IRQ_HUB_EP0_OUT_ACK_STALL |
  130. VHUB_IRQ_HUB_EP0_IN_ACK_STALL |
  131. VHUB_IRQ_HUB_EP0_SETUP)) {
  132. if (istat & VHUB_IRQ_HUB_EP0_IN_ACK_STALL)
  133. ast_vhub_ep0_handle_ack(&vhub->ep0, true);
  134. if (istat & VHUB_IRQ_HUB_EP0_OUT_ACK_STALL)
  135. ast_vhub_ep0_handle_ack(&vhub->ep0, false);
  136. if (istat & VHUB_IRQ_HUB_EP0_SETUP)
  137. ast_vhub_ep0_handle_setup(&vhub->ep0);
  138. }
  139. /* Various top level bus events */
  140. if (istat & (VHUB_IRQ_BUS_RESUME |
  141. VHUB_IRQ_BUS_SUSPEND |
  142. VHUB_IRQ_BUS_RESET)) {
  143. if (istat & VHUB_IRQ_BUS_RESUME)
  144. ast_vhub_hub_resume(vhub);
  145. if (istat & VHUB_IRQ_BUS_SUSPEND)
  146. ast_vhub_hub_suspend(vhub);
  147. if (istat & VHUB_IRQ_BUS_RESET)
  148. ast_vhub_hub_reset(vhub);
  149. }
  150. bail:
  151. spin_unlock(&vhub->lock);
  152. return iret;
  153. }
  154. void ast_vhub_init_hw(struct ast_vhub *vhub)
  155. {
  156. u32 ctrl;
  157. UDCDBG(vhub,"(Re)Starting HW ...\n");
  158. /* Enable PHY */
  159. ctrl = VHUB_CTRL_PHY_CLK |
  160. VHUB_CTRL_PHY_RESET_DIS;
  161. /*
  162. * We do *NOT* set the VHUB_CTRL_CLK_STOP_SUSPEND bit
  163. * to stop the logic clock during suspend because
  164. * it causes the registers to become inaccessible and
  165. * we haven't yet figured out a good wayt to bring the
  166. * controller back into life to issue a wakeup.
  167. */
  168. /*
  169. * Set some ISO & split control bits according to Aspeed
  170. * recommendation
  171. *
  172. * VHUB_CTRL_ISO_RSP_CTRL: When set tells the HW to respond
  173. * with 0 bytes data packet to ISO IN endpoints when no data
  174. * is available.
  175. *
  176. * VHUB_CTRL_SPLIT_IN: This makes a SOF complete a split IN
  177. * transaction.
  178. */
  179. ctrl |= VHUB_CTRL_ISO_RSP_CTRL | VHUB_CTRL_SPLIT_IN;
  180. writel(ctrl, vhub->regs + AST_VHUB_CTRL);
  181. udelay(1);
  182. /* Set descriptor ring size */
  183. if (AST_VHUB_DESCS_COUNT == 256) {
  184. ctrl |= VHUB_CTRL_LONG_DESC;
  185. writel(ctrl, vhub->regs + AST_VHUB_CTRL);
  186. } else {
  187. BUILD_BUG_ON(AST_VHUB_DESCS_COUNT != 32);
  188. }
  189. /* Reset all devices */
  190. writel(VHUB_SW_RESET_ALL, vhub->regs + AST_VHUB_SW_RESET);
  191. udelay(1);
  192. writel(0, vhub->regs + AST_VHUB_SW_RESET);
  193. /* Disable and cleanup EP ACK/NACK interrupts */
  194. writel(0, vhub->regs + AST_VHUB_EP_ACK_IER);
  195. writel(0, vhub->regs + AST_VHUB_EP_NACK_IER);
  196. writel(VHUB_EP_IRQ_ALL, vhub->regs + AST_VHUB_EP_ACK_ISR);
  197. writel(VHUB_EP_IRQ_ALL, vhub->regs + AST_VHUB_EP_NACK_ISR);
  198. /* Default settings for EP0, enable HW hub EP1 */
  199. writel(0, vhub->regs + AST_VHUB_EP0_CTRL);
  200. writel(VHUB_EP1_CTRL_RESET_TOGGLE |
  201. VHUB_EP1_CTRL_ENABLE,
  202. vhub->regs + AST_VHUB_EP1_CTRL);
  203. writel(0, vhub->regs + AST_VHUB_EP1_STS_CHG);
  204. /* Configure EP0 DMA buffer */
  205. writel(vhub->ep0.buf_dma, vhub->regs + AST_VHUB_EP0_DATA);
  206. /* Clear address */
  207. writel(0, vhub->regs + AST_VHUB_CONF);
  208. /* Pullup hub (activate on host) */
  209. if (vhub->force_usb1)
  210. ctrl |= VHUB_CTRL_FULL_SPEED_ONLY;
  211. ctrl |= VHUB_CTRL_UPSTREAM_CONNECT;
  212. writel(ctrl, vhub->regs + AST_VHUB_CTRL);
  213. /* Enable some interrupts */
  214. writel(VHUB_IRQ_HUB_EP0_IN_ACK_STALL |
  215. VHUB_IRQ_HUB_EP0_OUT_ACK_STALL |
  216. VHUB_IRQ_HUB_EP0_SETUP |
  217. VHUB_IRQ_EP_POOL_ACK_STALL |
  218. VHUB_IRQ_BUS_RESUME |
  219. VHUB_IRQ_BUS_SUSPEND |
  220. VHUB_IRQ_BUS_RESET,
  221. vhub->regs + AST_VHUB_IER);
  222. }
  223. static int ast_vhub_remove(struct platform_device *pdev)
  224. {
  225. struct ast_vhub *vhub = platform_get_drvdata(pdev);
  226. unsigned long flags;
  227. int i;
  228. if (!vhub || !vhub->regs)
  229. return 0;
  230. /* Remove devices */
  231. for (i = 0; i < AST_VHUB_NUM_PORTS; i++)
  232. ast_vhub_del_dev(&vhub->ports[i].dev);
  233. spin_lock_irqsave(&vhub->lock, flags);
  234. /* Mask & ack all interrupts */
  235. writel(0, vhub->regs + AST_VHUB_IER);
  236. writel(VHUB_IRQ_ACK_ALL, vhub->regs + AST_VHUB_ISR);
  237. /* Pull device, leave PHY enabled */
  238. writel(VHUB_CTRL_PHY_CLK |
  239. VHUB_CTRL_PHY_RESET_DIS,
  240. vhub->regs + AST_VHUB_CTRL);
  241. if (vhub->clk)
  242. clk_disable_unprepare(vhub->clk);
  243. spin_unlock_irqrestore(&vhub->lock, flags);
  244. if (vhub->ep0_bufs)
  245. dma_free_coherent(&pdev->dev,
  246. AST_VHUB_EP0_MAX_PACKET *
  247. (AST_VHUB_NUM_PORTS + 1),
  248. vhub->ep0_bufs,
  249. vhub->ep0_bufs_dma);
  250. vhub->ep0_bufs = NULL;
  251. return 0;
  252. }
  253. static int ast_vhub_probe(struct platform_device *pdev)
  254. {
  255. enum usb_device_speed max_speed;
  256. struct ast_vhub *vhub;
  257. struct resource *res;
  258. int i, rc = 0;
  259. vhub = devm_kzalloc(&pdev->dev, sizeof(*vhub), GFP_KERNEL);
  260. if (!vhub)
  261. return -ENOMEM;
  262. spin_lock_init(&vhub->lock);
  263. vhub->pdev = pdev;
  264. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  265. vhub->regs = devm_ioremap_resource(&pdev->dev, res);
  266. if (IS_ERR(vhub->regs)) {
  267. dev_err(&pdev->dev, "Failed to map resources\n");
  268. return PTR_ERR(vhub->regs);
  269. }
  270. UDCDBG(vhub, "vHub@%pR mapped @%p\n", res, vhub->regs);
  271. platform_set_drvdata(pdev, vhub);
  272. vhub->clk = devm_clk_get(&pdev->dev, NULL);
  273. if (IS_ERR(vhub->clk)) {
  274. rc = PTR_ERR(vhub->clk);
  275. goto err;
  276. }
  277. rc = clk_prepare_enable(vhub->clk);
  278. if (rc) {
  279. dev_err(&pdev->dev, "Error couldn't enable clock (%d)\n", rc);
  280. goto err;
  281. }
  282. /* Check if we need to limit the HW to USB1 */
  283. max_speed = usb_get_maximum_speed(&pdev->dev);
  284. if (max_speed != USB_SPEED_UNKNOWN && max_speed < USB_SPEED_HIGH)
  285. vhub->force_usb1 = true;
  286. /* Mask & ack all interrupts before installing the handler */
  287. writel(0, vhub->regs + AST_VHUB_IER);
  288. writel(VHUB_IRQ_ACK_ALL, vhub->regs + AST_VHUB_ISR);
  289. /* Find interrupt and install handler */
  290. vhub->irq = platform_get_irq(pdev, 0);
  291. if (vhub->irq < 0) {
  292. dev_err(&pdev->dev, "Failed to get interrupt\n");
  293. rc = vhub->irq;
  294. goto err;
  295. }
  296. rc = devm_request_irq(&pdev->dev, vhub->irq, ast_vhub_irq, 0,
  297. KBUILD_MODNAME, vhub);
  298. if (rc) {
  299. dev_err(&pdev->dev, "Failed to request interrupt\n");
  300. goto err;
  301. }
  302. /*
  303. * Allocate DMA buffers for all EP0s in one chunk,
  304. * one per port and one for the vHub itself
  305. */
  306. vhub->ep0_bufs = dma_alloc_coherent(&pdev->dev,
  307. AST_VHUB_EP0_MAX_PACKET *
  308. (AST_VHUB_NUM_PORTS + 1),
  309. &vhub->ep0_bufs_dma, GFP_KERNEL);
  310. if (!vhub->ep0_bufs) {
  311. dev_err(&pdev->dev, "Failed to allocate EP0 DMA buffers\n");
  312. rc = -ENOMEM;
  313. goto err;
  314. }
  315. UDCVDBG(vhub, "EP0 DMA buffers @%p (DMA 0x%08x)\n",
  316. vhub->ep0_bufs, (u32)vhub->ep0_bufs_dma);
  317. /* Init vHub EP0 */
  318. ast_vhub_init_ep0(vhub, &vhub->ep0, NULL);
  319. /* Init devices */
  320. for (i = 0; i < AST_VHUB_NUM_PORTS && rc == 0; i++)
  321. rc = ast_vhub_init_dev(vhub, i);
  322. if (rc)
  323. goto err;
  324. /* Init hub emulation */
  325. ast_vhub_init_hub(vhub);
  326. /* Initialize HW */
  327. ast_vhub_init_hw(vhub);
  328. dev_info(&pdev->dev, "Initialized virtual hub in USB%d mode\n",
  329. vhub->force_usb1 ? 1 : 2);
  330. return 0;
  331. err:
  332. ast_vhub_remove(pdev);
  333. return rc;
  334. }
  335. static const struct of_device_id ast_vhub_dt_ids[] = {
  336. {
  337. .compatible = "aspeed,ast2400-usb-vhub",
  338. },
  339. {
  340. .compatible = "aspeed,ast2500-usb-vhub",
  341. },
  342. { }
  343. };
  344. MODULE_DEVICE_TABLE(of, ast_vhub_dt_ids);
  345. static struct platform_driver ast_vhub_driver = {
  346. .probe = ast_vhub_probe,
  347. .remove = ast_vhub_remove,
  348. .driver = {
  349. .name = KBUILD_MODNAME,
  350. .of_match_table = ast_vhub_dt_ids,
  351. },
  352. };
  353. module_platform_driver(ast_vhub_driver);
  354. MODULE_DESCRIPTION("Aspeed vHub udc driver");
  355. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  356. MODULE_LICENSE("GPL");