1
2
3
4
5
6
7
8
9
10
11
12
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
56
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;
68
69 EPDBG(ep, "Nuking\n");
70
71
72 while (!list_empty(&ep->queue)) {
73 req = list_first_entry(&ep->queue, struct ast_vhub_req, queue);
74 ast_vhub_done(ep, req, status);
75 }
76}
77
78struct usb_request *ast_vhub_alloc_request(struct usb_ep *u_ep,
79 gfp_t gfp_flags)
80{
81 struct ast_vhub_req *req;
82
83 req = kzalloc(sizeof(*req), gfp_flags);
84 if (!req)
85 return NULL;
86 return &req->req;
87}
88
89void ast_vhub_free_request(struct usb_ep *u_ep, struct usb_request *u_req)
90{
91 struct ast_vhub_req *req = to_ast_req(u_req);
92
93 kfree(req);
94}
95
96static irqreturn_t ast_vhub_irq(int irq, void *data)
97{
98 struct ast_vhub *vhub = data;
99 irqreturn_t iret = IRQ_NONE;
100 u32 istat;
101
102
103 if (!vhub->ep0_bufs)
104 return IRQ_NONE;
105
106 spin_lock(&vhub->lock);
107
108
109 istat = readl(vhub->regs + AST_VHUB_ISR);
110 if (!istat)
111 goto bail;
112 writel(istat, vhub->regs + AST_VHUB_ISR);
113 iret = IRQ_HANDLED;
114
115 UDCVDBG(vhub, "irq status=%08x, ep_acks=%08x ep_nacks=%08x\n",
116 istat,
117 readl(vhub->regs + AST_VHUB_EP_ACK_ISR),
118 readl(vhub->regs + AST_VHUB_EP_NACK_ISR));
119
120
121 if (istat & VHUB_IRQ_EP_POOL_ACK_STALL) {
122 u32 i, ep_acks = readl(vhub->regs + AST_VHUB_EP_ACK_ISR);
123 writel(ep_acks, vhub->regs + AST_VHUB_EP_ACK_ISR);
124
125 for (i = 0; ep_acks && i < AST_VHUB_NUM_GEN_EPs; i++) {
126 u32 mask = VHUB_EP_IRQ(i);
127 if (ep_acks & mask) {
128 ast_vhub_epn_ack_irq(&vhub->epns[i]);
129 ep_acks &= ~mask;
130 }
131 }
132 }
133
134
135 if (istat & (VHUB_IRQ_DEVICE1 |
136 VHUB_IRQ_DEVICE2 |
137 VHUB_IRQ_DEVICE3 |
138 VHUB_IRQ_DEVICE4 |
139 VHUB_IRQ_DEVICE5)) {
140 if (istat & VHUB_IRQ_DEVICE1)
141 ast_vhub_dev_irq(&vhub->ports[0].dev);
142 if (istat & VHUB_IRQ_DEVICE2)
143 ast_vhub_dev_irq(&vhub->ports[1].dev);
144 if (istat & VHUB_IRQ_DEVICE3)
145 ast_vhub_dev_irq(&vhub->ports[2].dev);
146 if (istat & VHUB_IRQ_DEVICE4)
147 ast_vhub_dev_irq(&vhub->ports[3].dev);
148 if (istat & VHUB_IRQ_DEVICE5)
149 ast_vhub_dev_irq(&vhub->ports[4].dev);
150 }
151
152
153 if (istat & (VHUB_IRQ_HUB_EP0_OUT_ACK_STALL |
154 VHUB_IRQ_HUB_EP0_IN_ACK_STALL |
155 VHUB_IRQ_HUB_EP0_SETUP)) {
156 if (istat & VHUB_IRQ_HUB_EP0_IN_ACK_STALL)
157 ast_vhub_ep0_handle_ack(&vhub->ep0, true);
158 if (istat & VHUB_IRQ_HUB_EP0_OUT_ACK_STALL)
159 ast_vhub_ep0_handle_ack(&vhub->ep0, false);
160 if (istat & VHUB_IRQ_HUB_EP0_SETUP)
161 ast_vhub_ep0_handle_setup(&vhub->ep0);
162 }
163
164
165 if (istat & (VHUB_IRQ_BUS_RESUME |
166 VHUB_IRQ_BUS_SUSPEND |
167 VHUB_IRQ_BUS_RESET)) {
168 if (istat & VHUB_IRQ_BUS_RESUME)
169 ast_vhub_hub_resume(vhub);
170 if (istat & VHUB_IRQ_BUS_SUSPEND)
171 ast_vhub_hub_suspend(vhub);
172 if (istat & VHUB_IRQ_BUS_RESET)
173 ast_vhub_hub_reset(vhub);
174 }
175
176 bail:
177 spin_unlock(&vhub->lock);
178 return iret;
179}
180
181void ast_vhub_init_hw(struct ast_vhub *vhub)
182{
183 u32 ctrl;
184
185 UDCDBG(vhub,"(Re)Starting HW ...\n");
186
187
188 ctrl = VHUB_CTRL_PHY_CLK |
189 VHUB_CTRL_PHY_RESET_DIS;
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 ctrl |= VHUB_CTRL_ISO_RSP_CTRL | VHUB_CTRL_SPLIT_IN;
211 writel(ctrl, vhub->regs + AST_VHUB_CTRL);
212 udelay(1);
213
214
215 if (AST_VHUB_DESCS_COUNT == 256) {
216 ctrl |= VHUB_CTRL_LONG_DESC;
217 writel(ctrl, vhub->regs + AST_VHUB_CTRL);
218 } else {
219 BUILD_BUG_ON(AST_VHUB_DESCS_COUNT != 32);
220 }
221
222
223 writel(VHUB_SW_RESET_ALL, vhub->regs + AST_VHUB_SW_RESET);
224 udelay(1);
225 writel(0, vhub->regs + AST_VHUB_SW_RESET);
226
227
228 writel(0, vhub->regs + AST_VHUB_EP_ACK_IER);
229 writel(0, vhub->regs + AST_VHUB_EP_NACK_IER);
230 writel(VHUB_EP_IRQ_ALL, vhub->regs + AST_VHUB_EP_ACK_ISR);
231 writel(VHUB_EP_IRQ_ALL, vhub->regs + AST_VHUB_EP_NACK_ISR);
232
233
234 writel(0, vhub->regs + AST_VHUB_EP0_CTRL);
235 writel(VHUB_EP1_CTRL_RESET_TOGGLE |
236 VHUB_EP1_CTRL_ENABLE,
237 vhub->regs + AST_VHUB_EP1_CTRL);
238 writel(0, vhub->regs + AST_VHUB_EP1_STS_CHG);
239
240
241 writel(vhub->ep0.buf_dma, vhub->regs + AST_VHUB_EP0_DATA);
242
243
244 writel(0, vhub->regs + AST_VHUB_CONF);
245
246
247 if (vhub->force_usb1)
248 ctrl |= VHUB_CTRL_FULL_SPEED_ONLY;
249
250 ctrl |= VHUB_CTRL_UPSTREAM_CONNECT;
251 writel(ctrl, vhub->regs + AST_VHUB_CTRL);
252
253
254 writel(VHUB_IRQ_HUB_EP0_IN_ACK_STALL |
255 VHUB_IRQ_HUB_EP0_OUT_ACK_STALL |
256 VHUB_IRQ_HUB_EP0_SETUP |
257 VHUB_IRQ_EP_POOL_ACK_STALL |
258 VHUB_IRQ_BUS_RESUME |
259 VHUB_IRQ_BUS_SUSPEND |
260 VHUB_IRQ_BUS_RESET,
261 vhub->regs + AST_VHUB_IER);
262}
263
264static int ast_vhub_remove(struct platform_device *pdev)
265{
266 struct ast_vhub *vhub = platform_get_drvdata(pdev);
267 unsigned long flags;
268 int i;
269
270 if (!vhub || !vhub->regs)
271 return 0;
272
273
274 for (i = 0; i < AST_VHUB_NUM_PORTS; i++)
275 ast_vhub_del_dev(&vhub->ports[i].dev);
276
277 spin_lock_irqsave(&vhub->lock, flags);
278
279
280 writel(0, vhub->regs + AST_VHUB_IER);
281 writel(VHUB_IRQ_ACK_ALL, vhub->regs + AST_VHUB_ISR);
282
283
284 writel(VHUB_CTRL_PHY_CLK |
285 VHUB_CTRL_PHY_RESET_DIS,
286 vhub->regs + AST_VHUB_CTRL);
287
288 if (vhub->clk)
289 clk_disable_unprepare(vhub->clk);
290
291 spin_unlock_irqrestore(&vhub->lock, flags);
292
293 if (vhub->ep0_bufs)
294 dma_free_coherent(&pdev->dev,
295 AST_VHUB_EP0_MAX_PACKET *
296 (AST_VHUB_NUM_PORTS + 1),
297 vhub->ep0_bufs,
298 vhub->ep0_bufs_dma);
299 vhub->ep0_bufs = NULL;
300
301 return 0;
302}
303
304static int ast_vhub_probe(struct platform_device *pdev)
305{
306 enum usb_device_speed max_speed;
307 struct ast_vhub *vhub;
308 struct resource *res;
309 int i, rc = 0;
310
311 vhub = devm_kzalloc(&pdev->dev, sizeof(*vhub), GFP_KERNEL);
312 if (!vhub)
313 return -ENOMEM;
314
315 spin_lock_init(&vhub->lock);
316 vhub->pdev = pdev;
317
318 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
319 vhub->regs = devm_ioremap_resource(&pdev->dev, res);
320 if (IS_ERR(vhub->regs)) {
321 dev_err(&pdev->dev, "Failed to map resources\n");
322 return PTR_ERR(vhub->regs);
323 }
324 UDCDBG(vhub, "vHub@%pR mapped @%p\n", res, vhub->regs);
325
326 platform_set_drvdata(pdev, vhub);
327
328 vhub->clk = devm_clk_get(&pdev->dev, NULL);
329 if (IS_ERR(vhub->clk)) {
330 rc = PTR_ERR(vhub->clk);
331 goto err;
332 }
333 rc = clk_prepare_enable(vhub->clk);
334 if (rc) {
335 dev_err(&pdev->dev, "Error couldn't enable clock (%d)\n", rc);
336 goto err;
337 }
338
339
340 max_speed = usb_get_maximum_speed(&pdev->dev);
341 if (max_speed != USB_SPEED_UNKNOWN && max_speed < USB_SPEED_HIGH)
342 vhub->force_usb1 = true;
343
344
345 writel(0, vhub->regs + AST_VHUB_IER);
346 writel(VHUB_IRQ_ACK_ALL, vhub->regs + AST_VHUB_ISR);
347
348
349 vhub->irq = platform_get_irq(pdev, 0);
350 if (vhub->irq < 0) {
351 dev_err(&pdev->dev, "Failed to get interrupt\n");
352 rc = vhub->irq;
353 goto err;
354 }
355 rc = devm_request_irq(&pdev->dev, vhub->irq, ast_vhub_irq, 0,
356 KBUILD_MODNAME, vhub);
357 if (rc) {
358 dev_err(&pdev->dev, "Failed to request interrupt\n");
359 goto err;
360 }
361
362
363
364
365
366 vhub->ep0_bufs = dma_alloc_coherent(&pdev->dev,
367 AST_VHUB_EP0_MAX_PACKET *
368 (AST_VHUB_NUM_PORTS + 1),
369 &vhub->ep0_bufs_dma, GFP_KERNEL);
370 if (!vhub->ep0_bufs) {
371 dev_err(&pdev->dev, "Failed to allocate EP0 DMA buffers\n");
372 rc = -ENOMEM;
373 goto err;
374 }
375 UDCVDBG(vhub, "EP0 DMA buffers @%p (DMA 0x%08x)\n",
376 vhub->ep0_bufs, (u32)vhub->ep0_bufs_dma);
377
378
379 ast_vhub_init_ep0(vhub, &vhub->ep0, NULL);
380
381
382 for (i = 0; i < AST_VHUB_NUM_PORTS && rc == 0; i++)
383 rc = ast_vhub_init_dev(vhub, i);
384 if (rc)
385 goto err;
386
387
388 ast_vhub_init_hub(vhub);
389
390
391 ast_vhub_init_hw(vhub);
392
393 dev_info(&pdev->dev, "Initialized virtual hub in USB%d mode\n",
394 vhub->force_usb1 ? 1 : 2);
395
396 return 0;
397 err:
398 ast_vhub_remove(pdev);
399 return rc;
400}
401
402static const struct of_device_id ast_vhub_dt_ids[] = {
403 {
404 .compatible = "aspeed,ast2400-usb-vhub",
405 },
406 {
407 .compatible = "aspeed,ast2500-usb-vhub",
408 },
409 { }
410};
411MODULE_DEVICE_TABLE(of, ast_vhub_dt_ids);
412
413static struct platform_driver ast_vhub_driver = {
414 .probe = ast_vhub_probe,
415 .remove = ast_vhub_remove,
416 .driver = {
417 .name = KBUILD_MODNAME,
418 .of_match_table = ast_vhub_dt_ids,
419 },
420};
421module_platform_driver(ast_vhub_driver);
422
423MODULE_DESCRIPTION("Aspeed vHub udc driver");
424MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
425MODULE_LICENSE("GPL");
426