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