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