xref: /linux/drivers/usb/gadget/udc/aspeed-vhub/core.c (revision a1c613ae4c322ddd58d5a8539dbfba2a0380a8c0)
17ecca2a4SBenjamin Herrenschmidt // SPDX-License-Identifier: GPL-2.0+
27ecca2a4SBenjamin Herrenschmidt /*
37ecca2a4SBenjamin Herrenschmidt  * aspeed-vhub -- Driver for Aspeed SoC "vHub" USB gadget
47ecca2a4SBenjamin Herrenschmidt  *
57ecca2a4SBenjamin Herrenschmidt  * core.c - Top level support
67ecca2a4SBenjamin Herrenschmidt  *
77ecca2a4SBenjamin Herrenschmidt  * Copyright 2017 IBM Corporation
87ecca2a4SBenjamin Herrenschmidt  */
97ecca2a4SBenjamin Herrenschmidt 
107ecca2a4SBenjamin Herrenschmidt #include <linux/kernel.h>
117ecca2a4SBenjamin Herrenschmidt #include <linux/module.h>
127ecca2a4SBenjamin Herrenschmidt #include <linux/platform_device.h>
137ecca2a4SBenjamin Herrenschmidt #include <linux/delay.h>
147ecca2a4SBenjamin Herrenschmidt #include <linux/ioport.h>
157ecca2a4SBenjamin Herrenschmidt #include <linux/slab.h>
167ecca2a4SBenjamin Herrenschmidt #include <linux/errno.h>
177ecca2a4SBenjamin Herrenschmidt #include <linux/list.h>
187ecca2a4SBenjamin Herrenschmidt #include <linux/interrupt.h>
197ecca2a4SBenjamin Herrenschmidt #include <linux/proc_fs.h>
207ecca2a4SBenjamin Herrenschmidt #include <linux/prefetch.h>
217ecca2a4SBenjamin Herrenschmidt #include <linux/clk.h>
227ecca2a4SBenjamin Herrenschmidt #include <linux/usb/gadget.h>
237ecca2a4SBenjamin Herrenschmidt #include <linux/of.h>
247ecca2a4SBenjamin Herrenschmidt #include <linux/regmap.h>
257ecca2a4SBenjamin Herrenschmidt #include <linux/dma-mapping.h>
267ecca2a4SBenjamin Herrenschmidt 
277ecca2a4SBenjamin Herrenschmidt #include "vhub.h"
287ecca2a4SBenjamin Herrenschmidt 
ast_vhub_done(struct ast_vhub_ep * ep,struct ast_vhub_req * req,int status)297ecca2a4SBenjamin Herrenschmidt void ast_vhub_done(struct ast_vhub_ep *ep, struct ast_vhub_req *req,
307ecca2a4SBenjamin Herrenschmidt 		   int status)
317ecca2a4SBenjamin Herrenschmidt {
327ecca2a4SBenjamin Herrenschmidt 	bool internal = req->internal;
33bd4d6070STao Ren 	struct ast_vhub *vhub = ep->vhub;
347ecca2a4SBenjamin Herrenschmidt 
357ecca2a4SBenjamin Herrenschmidt 	EPVDBG(ep, "completing request @%p, status %d\n", req, status);
367ecca2a4SBenjamin Herrenschmidt 
377ecca2a4SBenjamin Herrenschmidt 	list_del_init(&req->queue);
387ecca2a4SBenjamin Herrenschmidt 
3983045e19SHenry Tian 	if ((req->req.status == -EINPROGRESS) ||  (status == -EOVERFLOW))
407ecca2a4SBenjamin Herrenschmidt 		req->req.status = status;
417ecca2a4SBenjamin Herrenschmidt 
427ecca2a4SBenjamin Herrenschmidt 	if (req->req.dma) {
437ecca2a4SBenjamin Herrenschmidt 		if (!WARN_ON(!ep->dev))
44bd4d6070STao Ren 			usb_gadget_unmap_request_by_dev(&vhub->pdev->dev,
457ecca2a4SBenjamin Herrenschmidt 						 &req->req, ep->epn.is_in);
467ecca2a4SBenjamin Herrenschmidt 		req->req.dma = 0;
477ecca2a4SBenjamin Herrenschmidt 	}
487ecca2a4SBenjamin Herrenschmidt 
497ecca2a4SBenjamin Herrenschmidt 	/*
507ecca2a4SBenjamin Herrenschmidt 	 * If this isn't an internal EP0 request, call the core
517ecca2a4SBenjamin Herrenschmidt 	 * to call the gadget completion.
527ecca2a4SBenjamin Herrenschmidt 	 */
537ecca2a4SBenjamin Herrenschmidt 	if (!internal) {
547ecca2a4SBenjamin Herrenschmidt 		spin_unlock(&ep->vhub->lock);
557ecca2a4SBenjamin Herrenschmidt 		usb_gadget_giveback_request(&ep->ep, &req->req);
567ecca2a4SBenjamin Herrenschmidt 		spin_lock(&ep->vhub->lock);
577ecca2a4SBenjamin Herrenschmidt 	}
587ecca2a4SBenjamin Herrenschmidt }
597ecca2a4SBenjamin Herrenschmidt 
ast_vhub_nuke(struct ast_vhub_ep * ep,int status)607ecca2a4SBenjamin Herrenschmidt void ast_vhub_nuke(struct ast_vhub_ep *ep, int status)
617ecca2a4SBenjamin Herrenschmidt {
627ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub_req *req;
63cca1754cSBenjamin Herrenschmidt 	int count = 0;
647ecca2a4SBenjamin Herrenschmidt 
657ecca2a4SBenjamin Herrenschmidt 	/* Beware, lock will be dropped & req-acquired by done() */
667ecca2a4SBenjamin Herrenschmidt 	while (!list_empty(&ep->queue)) {
677ecca2a4SBenjamin Herrenschmidt 		req = list_first_entry(&ep->queue, struct ast_vhub_req, queue);
687ecca2a4SBenjamin Herrenschmidt 		ast_vhub_done(ep, req, status);
69cca1754cSBenjamin Herrenschmidt 		count++;
707ecca2a4SBenjamin Herrenschmidt 	}
71cca1754cSBenjamin Herrenschmidt 	if (count)
72cca1754cSBenjamin Herrenschmidt 		EPDBG(ep, "Nuked %d request(s)\n", count);
737ecca2a4SBenjamin Herrenschmidt }
747ecca2a4SBenjamin Herrenschmidt 
ast_vhub_alloc_request(struct usb_ep * u_ep,gfp_t gfp_flags)757ecca2a4SBenjamin Herrenschmidt struct usb_request *ast_vhub_alloc_request(struct usb_ep *u_ep,
767ecca2a4SBenjamin Herrenschmidt 					   gfp_t gfp_flags)
777ecca2a4SBenjamin Herrenschmidt {
787ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub_req *req;
797ecca2a4SBenjamin Herrenschmidt 
807ecca2a4SBenjamin Herrenschmidt 	req = kzalloc(sizeof(*req), gfp_flags);
817ecca2a4SBenjamin Herrenschmidt 	if (!req)
827ecca2a4SBenjamin Herrenschmidt 		return NULL;
837ecca2a4SBenjamin Herrenschmidt 	return &req->req;
847ecca2a4SBenjamin Herrenschmidt }
857ecca2a4SBenjamin Herrenschmidt 
ast_vhub_free_request(struct usb_ep * u_ep,struct usb_request * u_req)867ecca2a4SBenjamin Herrenschmidt void ast_vhub_free_request(struct usb_ep *u_ep, struct usb_request *u_req)
877ecca2a4SBenjamin Herrenschmidt {
887ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub_req *req = to_ast_req(u_req);
897ecca2a4SBenjamin Herrenschmidt 
907ecca2a4SBenjamin Herrenschmidt 	kfree(req);
917ecca2a4SBenjamin Herrenschmidt }
927ecca2a4SBenjamin Herrenschmidt 
ast_vhub_irq(int irq,void * data)937ecca2a4SBenjamin Herrenschmidt static irqreturn_t ast_vhub_irq(int irq, void *data)
947ecca2a4SBenjamin Herrenschmidt {
957ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub *vhub = data;
967ecca2a4SBenjamin Herrenschmidt 	irqreturn_t iret = IRQ_NONE;
97487bc828STao Ren 	u32 i, istat;
987ecca2a4SBenjamin Herrenschmidt 
997ecca2a4SBenjamin Herrenschmidt 	/* Stale interrupt while tearing down */
1007ecca2a4SBenjamin Herrenschmidt 	if (!vhub->ep0_bufs)
1017ecca2a4SBenjamin Herrenschmidt 		return IRQ_NONE;
1027ecca2a4SBenjamin Herrenschmidt 
1037ecca2a4SBenjamin Herrenschmidt 	spin_lock(&vhub->lock);
1047ecca2a4SBenjamin Herrenschmidt 
1057ecca2a4SBenjamin Herrenschmidt 	/* Read and ACK interrupts */
1067ecca2a4SBenjamin Herrenschmidt 	istat = readl(vhub->regs + AST_VHUB_ISR);
1077ecca2a4SBenjamin Herrenschmidt 	if (!istat)
1087ecca2a4SBenjamin Herrenschmidt 		goto bail;
1097ecca2a4SBenjamin Herrenschmidt 	writel(istat, vhub->regs + AST_VHUB_ISR);
1107ecca2a4SBenjamin Herrenschmidt 	iret = IRQ_HANDLED;
1117ecca2a4SBenjamin Herrenschmidt 
1127ecca2a4SBenjamin Herrenschmidt 	UDCVDBG(vhub, "irq status=%08x, ep_acks=%08x ep_nacks=%08x\n",
1137ecca2a4SBenjamin Herrenschmidt 	       istat,
1147ecca2a4SBenjamin Herrenschmidt 	       readl(vhub->regs + AST_VHUB_EP_ACK_ISR),
1157ecca2a4SBenjamin Herrenschmidt 	       readl(vhub->regs + AST_VHUB_EP_NACK_ISR));
1167ecca2a4SBenjamin Herrenschmidt 
1177ecca2a4SBenjamin Herrenschmidt 	/* Handle generic EPs first */
1187ecca2a4SBenjamin Herrenschmidt 	if (istat & VHUB_IRQ_EP_POOL_ACK_STALL) {
119487bc828STao Ren 		u32 ep_acks = readl(vhub->regs + AST_VHUB_EP_ACK_ISR);
1207ecca2a4SBenjamin Herrenschmidt 		writel(ep_acks, vhub->regs + AST_VHUB_EP_ACK_ISR);
1217ecca2a4SBenjamin Herrenschmidt 
122487bc828STao Ren 		for (i = 0; ep_acks && i < vhub->max_epns; i++) {
1237ecca2a4SBenjamin Herrenschmidt 			u32 mask = VHUB_EP_IRQ(i);
1247ecca2a4SBenjamin Herrenschmidt 			if (ep_acks & mask) {
1257ecca2a4SBenjamin Herrenschmidt 				ast_vhub_epn_ack_irq(&vhub->epns[i]);
1267ecca2a4SBenjamin Herrenschmidt 				ep_acks &= ~mask;
1277ecca2a4SBenjamin Herrenschmidt 			}
1287ecca2a4SBenjamin Herrenschmidt 		}
1297ecca2a4SBenjamin Herrenschmidt 	}
1307ecca2a4SBenjamin Herrenschmidt 
1317ecca2a4SBenjamin Herrenschmidt 	/* Handle device interrupts */
132a23be4edSTao Ren 	if (istat & vhub->port_irq_mask) {
133a1c0169aSTao Ren 		for (i = 0; i < vhub->max_ports; i++) {
134a1c0169aSTao Ren 			if (istat & VHUB_DEV_IRQ(i))
135487bc828STao Ren 				ast_vhub_dev_irq(&vhub->ports[i].dev);
1367ecca2a4SBenjamin Herrenschmidt 		}
137a23be4edSTao Ren 	}
1387ecca2a4SBenjamin Herrenschmidt 
1397ecca2a4SBenjamin Herrenschmidt 	/* Handle top-level vHub EP0 interrupts */
1407ecca2a4SBenjamin Herrenschmidt 	if (istat & (VHUB_IRQ_HUB_EP0_OUT_ACK_STALL |
1417ecca2a4SBenjamin Herrenschmidt 		     VHUB_IRQ_HUB_EP0_IN_ACK_STALL |
1427ecca2a4SBenjamin Herrenschmidt 		     VHUB_IRQ_HUB_EP0_SETUP)) {
1437ecca2a4SBenjamin Herrenschmidt 		if (istat & VHUB_IRQ_HUB_EP0_IN_ACK_STALL)
1447ecca2a4SBenjamin Herrenschmidt 			ast_vhub_ep0_handle_ack(&vhub->ep0, true);
1457ecca2a4SBenjamin Herrenschmidt 		if (istat & VHUB_IRQ_HUB_EP0_OUT_ACK_STALL)
1467ecca2a4SBenjamin Herrenschmidt 			ast_vhub_ep0_handle_ack(&vhub->ep0, false);
1477ecca2a4SBenjamin Herrenschmidt 		if (istat & VHUB_IRQ_HUB_EP0_SETUP)
1487ecca2a4SBenjamin Herrenschmidt 			ast_vhub_ep0_handle_setup(&vhub->ep0);
1497ecca2a4SBenjamin Herrenschmidt 	}
1507ecca2a4SBenjamin Herrenschmidt 
1517ecca2a4SBenjamin Herrenschmidt 	/* Various top level bus events */
1527ecca2a4SBenjamin Herrenschmidt 	if (istat & (VHUB_IRQ_BUS_RESUME |
1537ecca2a4SBenjamin Herrenschmidt 		     VHUB_IRQ_BUS_SUSPEND |
1547ecca2a4SBenjamin Herrenschmidt 		     VHUB_IRQ_BUS_RESET)) {
1557ecca2a4SBenjamin Herrenschmidt 		if (istat & VHUB_IRQ_BUS_RESUME)
1567ecca2a4SBenjamin Herrenschmidt 			ast_vhub_hub_resume(vhub);
1577ecca2a4SBenjamin Herrenschmidt 		if (istat & VHUB_IRQ_BUS_SUSPEND)
1587ecca2a4SBenjamin Herrenschmidt 			ast_vhub_hub_suspend(vhub);
1597ecca2a4SBenjamin Herrenschmidt 		if (istat & VHUB_IRQ_BUS_RESET)
1607ecca2a4SBenjamin Herrenschmidt 			ast_vhub_hub_reset(vhub);
1617ecca2a4SBenjamin Herrenschmidt 	}
1627ecca2a4SBenjamin Herrenschmidt 
1637ecca2a4SBenjamin Herrenschmidt  bail:
1647ecca2a4SBenjamin Herrenschmidt 	spin_unlock(&vhub->lock);
1657ecca2a4SBenjamin Herrenschmidt 	return iret;
1667ecca2a4SBenjamin Herrenschmidt }
1677ecca2a4SBenjamin Herrenschmidt 
ast_vhub_init_hw(struct ast_vhub * vhub)1687ecca2a4SBenjamin Herrenschmidt void ast_vhub_init_hw(struct ast_vhub *vhub)
1697ecca2a4SBenjamin Herrenschmidt {
170487bc828STao Ren 	u32 ctrl, port_mask, epn_mask;
1717ecca2a4SBenjamin Herrenschmidt 
1727ecca2a4SBenjamin Herrenschmidt 	UDCDBG(vhub,"(Re)Starting HW ...\n");
1737ecca2a4SBenjamin Herrenschmidt 
1747ecca2a4SBenjamin Herrenschmidt 	/* Enable PHY */
1757ecca2a4SBenjamin Herrenschmidt 	ctrl = VHUB_CTRL_PHY_CLK |
1767ecca2a4SBenjamin Herrenschmidt 		VHUB_CTRL_PHY_RESET_DIS;
1777ecca2a4SBenjamin Herrenschmidt 
1787ecca2a4SBenjamin Herrenschmidt        /*
1797ecca2a4SBenjamin Herrenschmidt 	* We do *NOT* set the VHUB_CTRL_CLK_STOP_SUSPEND bit
1807ecca2a4SBenjamin Herrenschmidt 	* to stop the logic clock during suspend because
1817ecca2a4SBenjamin Herrenschmidt 	* it causes the registers to become inaccessible and
1827ecca2a4SBenjamin Herrenschmidt 	* we haven't yet figured out a good wayt to bring the
1837ecca2a4SBenjamin Herrenschmidt 	* controller back into life to issue a wakeup.
1847ecca2a4SBenjamin Herrenschmidt 	*/
1857ecca2a4SBenjamin Herrenschmidt 
1867ecca2a4SBenjamin Herrenschmidt 	/*
1877ecca2a4SBenjamin Herrenschmidt 	 * Set some ISO & split control bits according to Aspeed
1887ecca2a4SBenjamin Herrenschmidt 	 * recommendation
1897ecca2a4SBenjamin Herrenschmidt 	 *
1907ecca2a4SBenjamin Herrenschmidt 	 * VHUB_CTRL_ISO_RSP_CTRL: When set tells the HW to respond
1917ecca2a4SBenjamin Herrenschmidt 	 * with 0 bytes data packet to ISO IN endpoints when no data
1927ecca2a4SBenjamin Herrenschmidt 	 * is available.
1937ecca2a4SBenjamin Herrenschmidt 	 *
1947ecca2a4SBenjamin Herrenschmidt 	 * VHUB_CTRL_SPLIT_IN: This makes a SOF complete a split IN
1957ecca2a4SBenjamin Herrenschmidt 	 * transaction.
1967ecca2a4SBenjamin Herrenschmidt 	 */
1977ecca2a4SBenjamin Herrenschmidt 	ctrl |= VHUB_CTRL_ISO_RSP_CTRL | VHUB_CTRL_SPLIT_IN;
1987ecca2a4SBenjamin Herrenschmidt 	writel(ctrl, vhub->regs + AST_VHUB_CTRL);
1997ecca2a4SBenjamin Herrenschmidt 	udelay(1);
2007ecca2a4SBenjamin Herrenschmidt 
2017ecca2a4SBenjamin Herrenschmidt 	/* Set descriptor ring size */
2027ecca2a4SBenjamin Herrenschmidt 	if (AST_VHUB_DESCS_COUNT == 256) {
2037ecca2a4SBenjamin Herrenschmidt 		ctrl |= VHUB_CTRL_LONG_DESC;
2047ecca2a4SBenjamin Herrenschmidt 		writel(ctrl, vhub->regs + AST_VHUB_CTRL);
2057ecca2a4SBenjamin Herrenschmidt 	} else {
2067ecca2a4SBenjamin Herrenschmidt 		BUILD_BUG_ON(AST_VHUB_DESCS_COUNT != 32);
2077ecca2a4SBenjamin Herrenschmidt 	}
2087ecca2a4SBenjamin Herrenschmidt 
2097ecca2a4SBenjamin Herrenschmidt 	/* Reset all devices */
210487bc828STao Ren 	port_mask = GENMASK(vhub->max_ports, 1);
211487bc828STao Ren 	writel(VHUB_SW_RESET_ROOT_HUB |
212487bc828STao Ren 	       VHUB_SW_RESET_DMA_CONTROLLER |
213487bc828STao Ren 	       VHUB_SW_RESET_EP_POOL |
214487bc828STao Ren 	       port_mask, vhub->regs + AST_VHUB_SW_RESET);
2157ecca2a4SBenjamin Herrenschmidt 	udelay(1);
2167ecca2a4SBenjamin Herrenschmidt 	writel(0, vhub->regs + AST_VHUB_SW_RESET);
2177ecca2a4SBenjamin Herrenschmidt 
2187ecca2a4SBenjamin Herrenschmidt 	/* Disable and cleanup EP ACK/NACK interrupts */
219487bc828STao Ren 	epn_mask = GENMASK(vhub->max_epns - 1, 0);
2207ecca2a4SBenjamin Herrenschmidt 	writel(0, vhub->regs + AST_VHUB_EP_ACK_IER);
2217ecca2a4SBenjamin Herrenschmidt 	writel(0, vhub->regs + AST_VHUB_EP_NACK_IER);
222487bc828STao Ren 	writel(epn_mask, vhub->regs + AST_VHUB_EP_ACK_ISR);
223487bc828STao Ren 	writel(epn_mask, vhub->regs + AST_VHUB_EP_NACK_ISR);
2247ecca2a4SBenjamin Herrenschmidt 
2257ecca2a4SBenjamin Herrenschmidt 	/* Default settings for EP0, enable HW hub EP1 */
2267ecca2a4SBenjamin Herrenschmidt 	writel(0, vhub->regs + AST_VHUB_EP0_CTRL);
2277ecca2a4SBenjamin Herrenschmidt 	writel(VHUB_EP1_CTRL_RESET_TOGGLE |
2287ecca2a4SBenjamin Herrenschmidt 	       VHUB_EP1_CTRL_ENABLE,
2297ecca2a4SBenjamin Herrenschmidt 	       vhub->regs + AST_VHUB_EP1_CTRL);
2307ecca2a4SBenjamin Herrenschmidt 	writel(0, vhub->regs + AST_VHUB_EP1_STS_CHG);
2317ecca2a4SBenjamin Herrenschmidt 
2327ecca2a4SBenjamin Herrenschmidt 	/* Configure EP0 DMA buffer */
2337ecca2a4SBenjamin Herrenschmidt 	writel(vhub->ep0.buf_dma, vhub->regs + AST_VHUB_EP0_DATA);
2347ecca2a4SBenjamin Herrenschmidt 
2357ecca2a4SBenjamin Herrenschmidt 	/* Clear address */
2367ecca2a4SBenjamin Herrenschmidt 	writel(0, vhub->regs + AST_VHUB_CONF);
2377ecca2a4SBenjamin Herrenschmidt 
2387ecca2a4SBenjamin Herrenschmidt 	/* Pullup hub (activate on host) */
2397ecca2a4SBenjamin Herrenschmidt 	if (vhub->force_usb1)
2407ecca2a4SBenjamin Herrenschmidt 		ctrl |= VHUB_CTRL_FULL_SPEED_ONLY;
2417ecca2a4SBenjamin Herrenschmidt 
2427ecca2a4SBenjamin Herrenschmidt 	ctrl |= VHUB_CTRL_UPSTREAM_CONNECT;
2437ecca2a4SBenjamin Herrenschmidt 	writel(ctrl, vhub->regs + AST_VHUB_CTRL);
2447ecca2a4SBenjamin Herrenschmidt 
2457ecca2a4SBenjamin Herrenschmidt 	/* Enable some interrupts */
2467ecca2a4SBenjamin Herrenschmidt 	writel(VHUB_IRQ_HUB_EP0_IN_ACK_STALL |
2477ecca2a4SBenjamin Herrenschmidt 	       VHUB_IRQ_HUB_EP0_OUT_ACK_STALL |
2487ecca2a4SBenjamin Herrenschmidt 	       VHUB_IRQ_HUB_EP0_SETUP |
2497ecca2a4SBenjamin Herrenschmidt 	       VHUB_IRQ_EP_POOL_ACK_STALL |
2507ecca2a4SBenjamin Herrenschmidt 	       VHUB_IRQ_BUS_RESUME |
2517ecca2a4SBenjamin Herrenschmidt 	       VHUB_IRQ_BUS_SUSPEND |
2527ecca2a4SBenjamin Herrenschmidt 	       VHUB_IRQ_BUS_RESET,
2537ecca2a4SBenjamin Herrenschmidt 	       vhub->regs + AST_VHUB_IER);
2547ecca2a4SBenjamin Herrenschmidt }
2557ecca2a4SBenjamin Herrenschmidt 
ast_vhub_remove(struct platform_device * pdev)256ba170e19SUwe Kleine-König static void ast_vhub_remove(struct platform_device *pdev)
2577ecca2a4SBenjamin Herrenschmidt {
2587ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub *vhub = platform_get_drvdata(pdev);
2597ecca2a4SBenjamin Herrenschmidt 	unsigned long flags;
2607ecca2a4SBenjamin Herrenschmidt 	int i;
2617ecca2a4SBenjamin Herrenschmidt 
2627ecca2a4SBenjamin Herrenschmidt 	if (!vhub || !vhub->regs)
263ba170e19SUwe Kleine-König 		return;
2647ecca2a4SBenjamin Herrenschmidt 
2657ecca2a4SBenjamin Herrenschmidt 	/* Remove devices */
266487bc828STao Ren 	for (i = 0; i < vhub->max_ports; i++)
2677ecca2a4SBenjamin Herrenschmidt 		ast_vhub_del_dev(&vhub->ports[i].dev);
2687ecca2a4SBenjamin Herrenschmidt 
2697ecca2a4SBenjamin Herrenschmidt 	spin_lock_irqsave(&vhub->lock, flags);
2707ecca2a4SBenjamin Herrenschmidt 
2717ecca2a4SBenjamin Herrenschmidt 	/* Mask & ack all interrupts  */
2727ecca2a4SBenjamin Herrenschmidt 	writel(0, vhub->regs + AST_VHUB_IER);
2737ecca2a4SBenjamin Herrenschmidt 	writel(VHUB_IRQ_ACK_ALL, vhub->regs + AST_VHUB_ISR);
2747ecca2a4SBenjamin Herrenschmidt 
2757ecca2a4SBenjamin Herrenschmidt 	/* Pull device, leave PHY enabled */
2767ecca2a4SBenjamin Herrenschmidt 	writel(VHUB_CTRL_PHY_CLK |
2777ecca2a4SBenjamin Herrenschmidt 	       VHUB_CTRL_PHY_RESET_DIS,
2787ecca2a4SBenjamin Herrenschmidt 	       vhub->regs + AST_VHUB_CTRL);
2797ecca2a4SBenjamin Herrenschmidt 
2807ecca2a4SBenjamin Herrenschmidt 	if (vhub->clk)
2817ecca2a4SBenjamin Herrenschmidt 		clk_disable_unprepare(vhub->clk);
2827ecca2a4SBenjamin Herrenschmidt 
2837ecca2a4SBenjamin Herrenschmidt 	spin_unlock_irqrestore(&vhub->lock, flags);
2847ecca2a4SBenjamin Herrenschmidt 
2857ecca2a4SBenjamin Herrenschmidt 	if (vhub->ep0_bufs)
2867ecca2a4SBenjamin Herrenschmidt 		dma_free_coherent(&pdev->dev,
2877ecca2a4SBenjamin Herrenschmidt 				  AST_VHUB_EP0_MAX_PACKET *
288487bc828STao Ren 				  (vhub->max_ports + 1),
2897ecca2a4SBenjamin Herrenschmidt 				  vhub->ep0_bufs,
2907ecca2a4SBenjamin Herrenschmidt 				  vhub->ep0_bufs_dma);
2917ecca2a4SBenjamin Herrenschmidt 	vhub->ep0_bufs = NULL;
2927ecca2a4SBenjamin Herrenschmidt }
2937ecca2a4SBenjamin Herrenschmidt 
ast_vhub_probe(struct platform_device * pdev)2947ecca2a4SBenjamin Herrenschmidt static int ast_vhub_probe(struct platform_device *pdev)
2957ecca2a4SBenjamin Herrenschmidt {
2967ecca2a4SBenjamin Herrenschmidt 	enum usb_device_speed max_speed;
2977ecca2a4SBenjamin Herrenschmidt 	struct ast_vhub *vhub;
2987ecca2a4SBenjamin Herrenschmidt 	struct resource *res;
2997ecca2a4SBenjamin Herrenschmidt 	int i, rc = 0;
300487bc828STao Ren 	const struct device_node *np = pdev->dev.of_node;
3017ecca2a4SBenjamin Herrenschmidt 
3027ecca2a4SBenjamin Herrenschmidt 	vhub = devm_kzalloc(&pdev->dev, sizeof(*vhub), GFP_KERNEL);
3037ecca2a4SBenjamin Herrenschmidt 	if (!vhub)
3047ecca2a4SBenjamin Herrenschmidt 		return -ENOMEM;
3057ecca2a4SBenjamin Herrenschmidt 
306487bc828STao Ren 	rc = of_property_read_u32(np, "aspeed,vhub-downstream-ports",
307487bc828STao Ren 				  &vhub->max_ports);
308487bc828STao Ren 	if (rc < 0)
309487bc828STao Ren 		vhub->max_ports = AST_VHUB_NUM_PORTS;
310487bc828STao Ren 
311487bc828STao Ren 	vhub->ports = devm_kcalloc(&pdev->dev, vhub->max_ports,
312487bc828STao Ren 				   sizeof(*vhub->ports), GFP_KERNEL);
313487bc828STao Ren 	if (!vhub->ports)
314487bc828STao Ren 		return -ENOMEM;
315487bc828STao Ren 
316487bc828STao Ren 	rc = of_property_read_u32(np, "aspeed,vhub-generic-endpoints",
317487bc828STao Ren 				  &vhub->max_epns);
318487bc828STao Ren 	if (rc < 0)
319487bc828STao Ren 		vhub->max_epns = AST_VHUB_NUM_GEN_EPs;
320487bc828STao Ren 
321487bc828STao Ren 	vhub->epns = devm_kcalloc(&pdev->dev, vhub->max_epns,
322487bc828STao Ren 				  sizeof(*vhub->epns), GFP_KERNEL);
323487bc828STao Ren 	if (!vhub->epns)
324487bc828STao Ren 		return -ENOMEM;
325487bc828STao Ren 
3267ecca2a4SBenjamin Herrenschmidt 	spin_lock_init(&vhub->lock);
3277ecca2a4SBenjamin Herrenschmidt 	vhub->pdev = pdev;
328a23be4edSTao Ren 	vhub->port_irq_mask = GENMASK(VHUB_IRQ_DEV1_BIT + vhub->max_ports - 1,
329a23be4edSTao Ren 				      VHUB_IRQ_DEV1_BIT);
3307ecca2a4SBenjamin Herrenschmidt 
331*3318f9c1SYangtao Li 	vhub->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
3327ecca2a4SBenjamin Herrenschmidt 	if (IS_ERR(vhub->regs)) {
3337ecca2a4SBenjamin Herrenschmidt 		dev_err(&pdev->dev, "Failed to map resources\n");
3347ecca2a4SBenjamin Herrenschmidt 		return PTR_ERR(vhub->regs);
3357ecca2a4SBenjamin Herrenschmidt 	}
3367ecca2a4SBenjamin Herrenschmidt 	UDCDBG(vhub, "vHub@%pR mapped @%p\n", res, vhub->regs);
3377ecca2a4SBenjamin Herrenschmidt 
3387ecca2a4SBenjamin Herrenschmidt 	platform_set_drvdata(pdev, vhub);
3397ecca2a4SBenjamin Herrenschmidt 
3407ecca2a4SBenjamin Herrenschmidt 	vhub->clk = devm_clk_get(&pdev->dev, NULL);
3417ecca2a4SBenjamin Herrenschmidt 	if (IS_ERR(vhub->clk)) {
3427ecca2a4SBenjamin Herrenschmidt 		rc = PTR_ERR(vhub->clk);
3437ecca2a4SBenjamin Herrenschmidt 		goto err;
3447ecca2a4SBenjamin Herrenschmidt 	}
3457ecca2a4SBenjamin Herrenschmidt 	rc = clk_prepare_enable(vhub->clk);
3467ecca2a4SBenjamin Herrenschmidt 	if (rc) {
3477ecca2a4SBenjamin Herrenschmidt 		dev_err(&pdev->dev, "Error couldn't enable clock (%d)\n", rc);
3487ecca2a4SBenjamin Herrenschmidt 		goto err;
3497ecca2a4SBenjamin Herrenschmidt 	}
3507ecca2a4SBenjamin Herrenschmidt 
3517ecca2a4SBenjamin Herrenschmidt 	/* Check if we need to limit the HW to USB1 */
3527ecca2a4SBenjamin Herrenschmidt 	max_speed = usb_get_maximum_speed(&pdev->dev);
3537ecca2a4SBenjamin Herrenschmidt 	if (max_speed != USB_SPEED_UNKNOWN && max_speed < USB_SPEED_HIGH)
3547ecca2a4SBenjamin Herrenschmidt 		vhub->force_usb1 = true;
3557ecca2a4SBenjamin Herrenschmidt 
3567ecca2a4SBenjamin Herrenschmidt 	/* Mask & ack all interrupts before installing the handler */
3577ecca2a4SBenjamin Herrenschmidt 	writel(0, vhub->regs + AST_VHUB_IER);
3587ecca2a4SBenjamin Herrenschmidt 	writel(VHUB_IRQ_ACK_ALL, vhub->regs + AST_VHUB_ISR);
3597ecca2a4SBenjamin Herrenschmidt 
3607ecca2a4SBenjamin Herrenschmidt 	/* Find interrupt and install handler */
3617ecca2a4SBenjamin Herrenschmidt 	vhub->irq = platform_get_irq(pdev, 0);
3627ecca2a4SBenjamin Herrenschmidt 	if (vhub->irq < 0) {
3637ecca2a4SBenjamin Herrenschmidt 		rc = vhub->irq;
3647ecca2a4SBenjamin Herrenschmidt 		goto err;
3657ecca2a4SBenjamin Herrenschmidt 	}
3667ecca2a4SBenjamin Herrenschmidt 	rc = devm_request_irq(&pdev->dev, vhub->irq, ast_vhub_irq, 0,
3677ecca2a4SBenjamin Herrenschmidt 			      KBUILD_MODNAME, vhub);
3687ecca2a4SBenjamin Herrenschmidt 	if (rc) {
3697ecca2a4SBenjamin Herrenschmidt 		dev_err(&pdev->dev, "Failed to request interrupt\n");
3707ecca2a4SBenjamin Herrenschmidt 		goto err;
3717ecca2a4SBenjamin Herrenschmidt 	}
3727ecca2a4SBenjamin Herrenschmidt 
3737ecca2a4SBenjamin Herrenschmidt 	/*
3747ecca2a4SBenjamin Herrenschmidt 	 * Allocate DMA buffers for all EP0s in one chunk,
3757ecca2a4SBenjamin Herrenschmidt 	 * one per port and one for the vHub itself
3767ecca2a4SBenjamin Herrenschmidt 	 */
3777ecca2a4SBenjamin Herrenschmidt 	vhub->ep0_bufs = dma_alloc_coherent(&pdev->dev,
3787ecca2a4SBenjamin Herrenschmidt 					    AST_VHUB_EP0_MAX_PACKET *
379487bc828STao Ren 					    (vhub->max_ports + 1),
3807ecca2a4SBenjamin Herrenschmidt 					    &vhub->ep0_bufs_dma, GFP_KERNEL);
3817ecca2a4SBenjamin Herrenschmidt 	if (!vhub->ep0_bufs) {
3827ecca2a4SBenjamin Herrenschmidt 		dev_err(&pdev->dev, "Failed to allocate EP0 DMA buffers\n");
3837ecca2a4SBenjamin Herrenschmidt 		rc = -ENOMEM;
3847ecca2a4SBenjamin Herrenschmidt 		goto err;
3857ecca2a4SBenjamin Herrenschmidt 	}
3867ecca2a4SBenjamin Herrenschmidt 	UDCVDBG(vhub, "EP0 DMA buffers @%p (DMA 0x%08x)\n",
3877ecca2a4SBenjamin Herrenschmidt 		vhub->ep0_bufs, (u32)vhub->ep0_bufs_dma);
3887ecca2a4SBenjamin Herrenschmidt 
3897ecca2a4SBenjamin Herrenschmidt 	/* Init vHub EP0 */
3907ecca2a4SBenjamin Herrenschmidt 	ast_vhub_init_ep0(vhub, &vhub->ep0, NULL);
3917ecca2a4SBenjamin Herrenschmidt 
3927ecca2a4SBenjamin Herrenschmidt 	/* Init devices */
393487bc828STao Ren 	for (i = 0; i < vhub->max_ports && rc == 0; i++)
3947ecca2a4SBenjamin Herrenschmidt 		rc = ast_vhub_init_dev(vhub, i);
3957ecca2a4SBenjamin Herrenschmidt 	if (rc)
3967ecca2a4SBenjamin Herrenschmidt 		goto err;
3977ecca2a4SBenjamin Herrenschmidt 
3987ecca2a4SBenjamin Herrenschmidt 	/* Init hub emulation */
3995cc0710fSTao Ren 	rc = ast_vhub_init_hub(vhub);
4005cc0710fSTao Ren 	if (rc)
4015cc0710fSTao Ren 		goto err;
4027ecca2a4SBenjamin Herrenschmidt 
4037ecca2a4SBenjamin Herrenschmidt 	/* Initialize HW */
4047ecca2a4SBenjamin Herrenschmidt 	ast_vhub_init_hw(vhub);
4057ecca2a4SBenjamin Herrenschmidt 
4067ecca2a4SBenjamin Herrenschmidt 	dev_info(&pdev->dev, "Initialized virtual hub in USB%d mode\n",
4077ecca2a4SBenjamin Herrenschmidt 		 vhub->force_usb1 ? 1 : 2);
4087ecca2a4SBenjamin Herrenschmidt 
4097ecca2a4SBenjamin Herrenschmidt 	return 0;
4107ecca2a4SBenjamin Herrenschmidt  err:
4117ecca2a4SBenjamin Herrenschmidt 	ast_vhub_remove(pdev);
4127ecca2a4SBenjamin Herrenschmidt 	return rc;
4137ecca2a4SBenjamin Herrenschmidt }
4147ecca2a4SBenjamin Herrenschmidt 
4157ecca2a4SBenjamin Herrenschmidt static const struct of_device_id ast_vhub_dt_ids[] = {
4167ecca2a4SBenjamin Herrenschmidt 	{
4177ecca2a4SBenjamin Herrenschmidt 		.compatible = "aspeed,ast2400-usb-vhub",
4187ecca2a4SBenjamin Herrenschmidt 	},
4197ecca2a4SBenjamin Herrenschmidt 	{
4207ecca2a4SBenjamin Herrenschmidt 		.compatible = "aspeed,ast2500-usb-vhub",
4217ecca2a4SBenjamin Herrenschmidt 	},
422b9a57990STao Ren 	{
423b9a57990STao Ren 		.compatible = "aspeed,ast2600-usb-vhub",
424b9a57990STao Ren 	},
4257ecca2a4SBenjamin Herrenschmidt 	{ }
4267ecca2a4SBenjamin Herrenschmidt };
4277ecca2a4SBenjamin Herrenschmidt MODULE_DEVICE_TABLE(of, ast_vhub_dt_ids);
4287ecca2a4SBenjamin Herrenschmidt 
4297ecca2a4SBenjamin Herrenschmidt static struct platform_driver ast_vhub_driver = {
4307ecca2a4SBenjamin Herrenschmidt 	.probe		= ast_vhub_probe,
431ba170e19SUwe Kleine-König 	.remove_new	= ast_vhub_remove,
4327ecca2a4SBenjamin Herrenschmidt 	.driver		= {
4337ecca2a4SBenjamin Herrenschmidt 		.name	= KBUILD_MODNAME,
4347ecca2a4SBenjamin Herrenschmidt 		.of_match_table	= ast_vhub_dt_ids,
4357ecca2a4SBenjamin Herrenschmidt 	},
4367ecca2a4SBenjamin Herrenschmidt };
4377ecca2a4SBenjamin Herrenschmidt module_platform_driver(ast_vhub_driver);
4387ecca2a4SBenjamin Herrenschmidt 
4397ecca2a4SBenjamin Herrenschmidt MODULE_DESCRIPTION("Aspeed vHub udc driver");
4407ecca2a4SBenjamin Herrenschmidt MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
4417ecca2a4SBenjamin Herrenschmidt MODULE_LICENSE("GPL");
442