]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/usb/chipidea/core.c
13dbd511f59aa093515bb3e1fa13dee58bbdf2dc
[karo-tx-linux.git] / drivers / usb / chipidea / core.c
1 /*
2  * core.c - ChipIdea USB IP core family device controller
3  *
4  * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved.
5  *
6  * Author: David Lopo
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 /*
14  * Description: ChipIdea USB IP core family device controller
15  *
16  * This driver is composed of several blocks:
17  * - HW:     hardware interface
18  * - DBG:    debug facilities (optional)
19  * - UTIL:   utilities
20  * - ISR:    interrupts handling
21  * - ENDPT:  endpoint operations (Gadget API)
22  * - GADGET: gadget operations (Gadget API)
23  * - BUS:    bus glue code, bus abstraction layer
24  *
25  * Compile Options
26  * - CONFIG_USB_GADGET_DEBUG_FILES: enable debug facilities
27  * - STALL_IN:  non-empty bulk-in pipes cannot be halted
28  *              if defined mass storage compliance succeeds but with warnings
29  *              => case 4: Hi >  Dn
30  *              => case 5: Hi >  Di
31  *              => case 8: Hi <> Do
32  *              if undefined usbtest 13 fails
33  * - TRACE:     enable function tracing (depends on DEBUG)
34  *
35  * Main Features
36  * - Chapter 9 & Mass Storage Compliance with Gadget File Storage
37  * - Chapter 9 Compliance with Gadget Zero (STALL_IN undefined)
38  * - Normal & LPM support
39  *
40  * USBTEST Report
41  * - OK: 0-12, 13 (STALL_IN defined) & 14
42  * - Not Supported: 15 & 16 (ISO)
43  *
44  * TODO List
45  * - OTG
46  * - Interrupt Traffic
47  * - GET_STATUS(device) - always reports 0
48  * - Gadget API (majority of optional features)
49  * - Suspend & Remote Wakeup
50  */
51 #include <linux/delay.h>
52 #include <linux/device.h>
53 #include <linux/dma-mapping.h>
54 #include <linux/platform_device.h>
55 #include <linux/module.h>
56 #include <linux/idr.h>
57 #include <linux/interrupt.h>
58 #include <linux/io.h>
59 #include <linux/kernel.h>
60 #include <linux/slab.h>
61 #include <linux/pm_runtime.h>
62 #include <linux/usb/ch9.h>
63 #include <linux/usb/gadget.h>
64 #include <linux/usb/otg.h>
65 #include <linux/usb/chipidea.h>
66 #include <linux/usb/of.h>
67 #include <linux/phy.h>
68 #include <linux/regulator/consumer.h>
69
70 #include "ci.h"
71 #include "udc.h"
72 #include "bits.h"
73 #include "host.h"
74 #include "debug.h"
75
76 /* Controller register map */
77 static uintptr_t ci_regs_nolpm[] = {
78         [CAP_CAPLENGTH]         = 0x000UL,
79         [CAP_HCCPARAMS]         = 0x008UL,
80         [CAP_DCCPARAMS]         = 0x024UL,
81         [CAP_TESTMODE]          = 0x038UL,
82         [OP_USBCMD]             = 0x000UL,
83         [OP_USBSTS]             = 0x004UL,
84         [OP_USBINTR]            = 0x008UL,
85         [OP_DEVICEADDR]         = 0x014UL,
86         [OP_ENDPTLISTADDR]      = 0x018UL,
87         [OP_PORTSC]             = 0x044UL,
88         [OP_DEVLC]              = 0x084UL,
89         [OP_OTGSC]              = 0x064UL,
90         [OP_USBMODE]            = 0x068UL,
91         [OP_ENDPTSETUPSTAT]     = 0x06CUL,
92         [OP_ENDPTPRIME]         = 0x070UL,
93         [OP_ENDPTFLUSH]         = 0x074UL,
94         [OP_ENDPTSTAT]          = 0x078UL,
95         [OP_ENDPTCOMPLETE]      = 0x07CUL,
96         [OP_ENDPTCTRL]          = 0x080UL,
97 };
98
99 static uintptr_t ci_regs_lpm[] = {
100         [CAP_CAPLENGTH]         = 0x000UL,
101         [CAP_HCCPARAMS]         = 0x008UL,
102         [CAP_DCCPARAMS]         = 0x024UL,
103         [CAP_TESTMODE]          = 0x0FCUL,
104         [OP_USBCMD]             = 0x000UL,
105         [OP_USBSTS]             = 0x004UL,
106         [OP_USBINTR]            = 0x008UL,
107         [OP_DEVICEADDR]         = 0x014UL,
108         [OP_ENDPTLISTADDR]      = 0x018UL,
109         [OP_PORTSC]             = 0x044UL,
110         [OP_DEVLC]              = 0x084UL,
111         [OP_OTGSC]              = 0x0C4UL,
112         [OP_USBMODE]            = 0x0C8UL,
113         [OP_ENDPTSETUPSTAT]     = 0x0D8UL,
114         [OP_ENDPTPRIME]         = 0x0DCUL,
115         [OP_ENDPTFLUSH]         = 0x0E0UL,
116         [OP_ENDPTSTAT]          = 0x0E4UL,
117         [OP_ENDPTCOMPLETE]      = 0x0E8UL,
118         [OP_ENDPTCTRL]          = 0x0ECUL,
119 };
120
121 static int hw_alloc_regmap(struct ci_hdrc *ci, bool is_lpm)
122 {
123         int i;
124
125         kfree(ci->hw_bank.regmap);
126
127         ci->hw_bank.regmap = kzalloc((OP_LAST + 1) * sizeof(void *),
128                                      GFP_KERNEL);
129         if (!ci->hw_bank.regmap)
130                 return -ENOMEM;
131
132         for (i = 0; i < OP_ENDPTCTRL; i++)
133                 ci->hw_bank.regmap[i] =
134                         (i <= CAP_LAST ? ci->hw_bank.cap : ci->hw_bank.op) +
135                         (is_lpm ? ci_regs_lpm[i] : ci_regs_nolpm[i]);
136
137         for (; i <= OP_LAST; i++)
138                 ci->hw_bank.regmap[i] = ci->hw_bank.op +
139                         4 * (i - OP_ENDPTCTRL) +
140                         (is_lpm
141                          ? ci_regs_lpm[OP_ENDPTCTRL]
142                          : ci_regs_nolpm[OP_ENDPTCTRL]);
143
144         return 0;
145 }
146
147 /**
148  * hw_port_test_set: writes port test mode (execute without interruption)
149  * @mode: new value
150  *
151  * This function returns an error code
152  */
153 int hw_port_test_set(struct ci_hdrc *ci, u8 mode)
154 {
155         const u8 TEST_MODE_MAX = 7;
156
157         if (mode > TEST_MODE_MAX)
158                 return -EINVAL;
159
160         hw_write(ci, OP_PORTSC, PORTSC_PTC, mode << __ffs(PORTSC_PTC));
161         return 0;
162 }
163
164 /**
165  * hw_port_test_get: reads port test mode value
166  *
167  * This function returns port test mode value
168  */
169 u8 hw_port_test_get(struct ci_hdrc *ci)
170 {
171         return hw_read(ci, OP_PORTSC, PORTSC_PTC) >> __ffs(PORTSC_PTC);
172 }
173
174 static int hw_device_init(struct ci_hdrc *ci, void __iomem *base)
175 {
176         u32 reg;
177
178         /* bank is a module variable */
179         ci->hw_bank.abs = base;
180
181         ci->hw_bank.cap = ci->hw_bank.abs;
182         ci->hw_bank.cap += ci->platdata->capoffset;
183         ci->hw_bank.op = ci->hw_bank.cap + (ioread32(ci->hw_bank.cap) & 0xff);
184
185         hw_alloc_regmap(ci, false);
186         reg = hw_read(ci, CAP_HCCPARAMS, HCCPARAMS_LEN) >>
187                 __ffs(HCCPARAMS_LEN);
188         ci->hw_bank.lpm  = reg;
189         hw_alloc_regmap(ci, !!reg);
190         ci->hw_bank.size = ci->hw_bank.op - ci->hw_bank.abs;
191         ci->hw_bank.size += OP_LAST;
192         ci->hw_bank.size /= sizeof(u32);
193
194         reg = hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DEN) >>
195                 __ffs(DCCPARAMS_DEN);
196         ci->hw_ep_max = reg * 2;   /* cache hw ENDPT_MAX */
197
198         if (ci->hw_ep_max > ENDPT_MAX)
199                 return -ENODEV;
200
201         dev_dbg(ci->dev, "ChipIdea HDRC found, lpm: %d; cap: %p op: %p\n",
202                 ci->hw_bank.lpm, ci->hw_bank.cap, ci->hw_bank.op);
203
204         /* setup lock mode ? */
205
206         /* ENDPTSETUPSTAT is '0' by default */
207
208         /* HCSPARAMS.bf.ppc SHOULD BE zero for device */
209
210         return 0;
211 }
212
213 static void hw_phymode_configure(struct ci_hdrc *ci)
214 {
215         u32 portsc, lpm, sts;
216
217         switch (ci->platdata->phy_mode) {
218         case USBPHY_INTERFACE_MODE_UTMI:
219                 portsc = PORTSC_PTS(PTS_UTMI);
220                 lpm = DEVLC_PTS(PTS_UTMI);
221                 break;
222         case USBPHY_INTERFACE_MODE_UTMIW:
223                 portsc = PORTSC_PTS(PTS_UTMI) | PORTSC_PTW;
224                 lpm = DEVLC_PTS(PTS_UTMI) | DEVLC_PTW;
225                 break;
226         case USBPHY_INTERFACE_MODE_ULPI:
227                 portsc = PORTSC_PTS(PTS_ULPI);
228                 lpm = DEVLC_PTS(PTS_ULPI);
229                 break;
230         case USBPHY_INTERFACE_MODE_SERIAL:
231                 portsc = PORTSC_PTS(PTS_SERIAL);
232                 lpm = DEVLC_PTS(PTS_SERIAL);
233                 sts = 1;
234                 break;
235         case USBPHY_INTERFACE_MODE_HSIC:
236                 portsc = PORTSC_PTS(PTS_HSIC);
237                 lpm = DEVLC_PTS(PTS_HSIC);
238                 break;
239         default:
240                 return;
241         }
242
243         if (ci->hw_bank.lpm) {
244                 hw_write(ci, OP_DEVLC, DEVLC_PTS(7) | DEVLC_PTW, lpm);
245                 hw_write(ci, OP_DEVLC, DEVLC_STS, sts);
246         } else {
247                 hw_write(ci, OP_PORTSC, PORTSC_PTS(7) | PORTSC_PTW, portsc);
248                 hw_write(ci, OP_PORTSC, PORTSC_STS, sts);
249         }
250 }
251
252 /**
253  * hw_device_reset: resets chip (execute without interruption)
254  * @ci: the controller
255   *
256  * This function returns an error code
257  */
258 int hw_device_reset(struct ci_hdrc *ci, u32 mode)
259 {
260         /* should flush & stop before reset */
261         hw_write(ci, OP_ENDPTFLUSH, ~0, ~0);
262         hw_write(ci, OP_USBCMD, USBCMD_RS, 0);
263
264         hw_write(ci, OP_USBCMD, USBCMD_RST, USBCMD_RST);
265         while (hw_read(ci, OP_USBCMD, USBCMD_RST))
266                 udelay(10);             /* not RTOS friendly */
267
268         if (ci->platdata->notify_event)
269                 ci->platdata->notify_event(ci,
270                         CI_HDRC_CONTROLLER_RESET_EVENT);
271
272         if (ci->platdata->flags & CI_HDRC_DISABLE_STREAMING)
273                 hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS, USBMODE_CI_SDIS);
274
275         /* USBMODE should be configured step by step */
276         hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE);
277         hw_write(ci, OP_USBMODE, USBMODE_CM, mode);
278         /* HW >= 2.3 */
279         hw_write(ci, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM);
280
281         if (hw_read(ci, OP_USBMODE, USBMODE_CM) != mode) {
282                 pr_err("cannot enter in %s mode", ci_role(ci)->name);
283                 pr_err("lpm = %i", ci->hw_bank.lpm);
284                 return -ENODEV;
285         }
286
287         return 0;
288 }
289
290 /**
291  * ci_otg_role - pick role based on ID pin state
292  * @ci: the controller
293  */
294 static enum ci_role ci_otg_role(struct ci_hdrc *ci)
295 {
296         u32 sts = hw_read(ci, OP_OTGSC, ~0);
297         enum ci_role role = sts & OTGSC_ID
298                 ? CI_ROLE_GADGET
299                 : CI_ROLE_HOST;
300
301         return role;
302 }
303
304 /**
305  * ci_role_work - perform role changing based on ID pin
306  * @work: work struct
307  */
308 static void ci_role_work(struct work_struct *work)
309 {
310         struct ci_hdrc *ci = container_of(work, struct ci_hdrc, work);
311         enum ci_role role = ci_otg_role(ci);
312
313         if (role != ci->role) {
314                 dev_dbg(ci->dev, "switching from %s to %s\n",
315                         ci_role(ci)->name, ci->roles[role]->name);
316
317                 ci_role_stop(ci);
318                 ci_role_start(ci, role);
319         }
320
321         enable_irq(ci->irq);
322 }
323
324 static irqreturn_t ci_irq(int irq, void *data)
325 {
326         struct ci_hdrc *ci = data;
327         irqreturn_t ret = IRQ_NONE;
328         u32 otgsc = 0;
329
330         if (ci->is_otg)
331                 otgsc = hw_read(ci, OP_OTGSC, ~0);
332
333         if (ci->role != CI_ROLE_END)
334                 ret = ci_role(ci)->irq(ci);
335
336         if (ci->is_otg && (otgsc & OTGSC_IDIS)) {
337                 hw_write(ci, OP_OTGSC, OTGSC_IDIS, OTGSC_IDIS);
338                 disable_irq_nosync(ci->irq);
339                 queue_work(ci->wq, &ci->work);
340                 ret = IRQ_HANDLED;
341         }
342
343         return ret;
344 }
345
346 static int ci_get_platdata(struct device *dev,
347                 struct ci_hdrc_platform_data *platdata)
348 {
349         /* Get the vbus regulator */
350         platdata->reg_vbus = devm_regulator_get(dev, "vbus");
351         if (PTR_ERR(platdata->reg_vbus) == -EPROBE_DEFER) {
352                 return -EPROBE_DEFER;
353         } else if (PTR_ERR(platdata->reg_vbus) == -ENODEV) {
354                 platdata->reg_vbus = NULL; /* no vbus regualator is needed */
355         } else if (IS_ERR(platdata->reg_vbus)) {
356                 dev_err(dev, "Getting regulator error: %ld\n",
357                         PTR_ERR(platdata->reg_vbus));
358                 return PTR_ERR(platdata->reg_vbus);
359         }
360
361         return 0;
362 }
363
364 static DEFINE_IDA(ci_ida);
365
366 struct platform_device *ci_hdrc_add_device(struct device *dev,
367                         struct resource *res, int nres,
368                         struct ci_hdrc_platform_data *platdata)
369 {
370         struct platform_device *pdev;
371         int id, ret;
372
373         ret = ci_get_platdata(dev, platdata);
374         if (ret)
375                 return ERR_PTR(ret);
376
377         id = ida_simple_get(&ci_ida, 0, 0, GFP_KERNEL);
378         if (id < 0)
379                 return ERR_PTR(id);
380
381         pdev = platform_device_alloc("ci_hdrc", id);
382         if (!pdev) {
383                 ret = -ENOMEM;
384                 goto put_id;
385         }
386
387         pdev->dev.parent = dev;
388         pdev->dev.dma_mask = dev->dma_mask;
389         pdev->dev.dma_parms = dev->dma_parms;
390         dma_set_coherent_mask(&pdev->dev, dev->coherent_dma_mask);
391
392         ret = platform_device_add_resources(pdev, res, nres);
393         if (ret)
394                 goto err;
395
396         ret = platform_device_add_data(pdev, platdata, sizeof(*platdata));
397         if (ret)
398                 goto err;
399
400         ret = platform_device_add(pdev);
401         if (ret)
402                 goto err;
403
404         return pdev;
405
406 err:
407         platform_device_put(pdev);
408 put_id:
409         ida_simple_remove(&ci_ida, id);
410         return ERR_PTR(ret);
411 }
412 EXPORT_SYMBOL_GPL(ci_hdrc_add_device);
413
414 void ci_hdrc_remove_device(struct platform_device *pdev)
415 {
416         int id = pdev->id;
417         platform_device_unregister(pdev);
418         ida_simple_remove(&ci_ida, id);
419 }
420 EXPORT_SYMBOL_GPL(ci_hdrc_remove_device);
421
422 static int ci_hdrc_probe(struct platform_device *pdev)
423 {
424         struct device   *dev = &pdev->dev;
425         struct ci_hdrc  *ci;
426         struct resource *res;
427         void __iomem    *base;
428         int             ret;
429         enum usb_dr_mode dr_mode;
430         struct device_node *of_node = dev->of_node ?: dev->parent->of_node;
431
432         if (!dev->platform_data) {
433                 dev_err(dev, "platform data missing\n");
434                 return -ENODEV;
435         }
436
437         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
438         base = devm_ioremap_resource(dev, res);
439         if (IS_ERR(base))
440                 return PTR_ERR(base);
441
442         ci = devm_kzalloc(dev, sizeof(*ci), GFP_KERNEL);
443         if (!ci) {
444                 dev_err(dev, "can't allocate device\n");
445                 return -ENOMEM;
446         }
447
448         ci->dev = dev;
449         ci->platdata = dev->platform_data;
450         if (ci->platdata->phy)
451                 ci->transceiver = ci->platdata->phy;
452         else
453                 ci->global_phy = true;
454
455         ret = hw_device_init(ci, base);
456         if (ret < 0) {
457                 dev_err(dev, "can't initialize hardware\n");
458                 return -ENODEV;
459         }
460
461         ci->hw_bank.phys = res->start;
462
463         ci->irq = platform_get_irq(pdev, 0);
464         if (ci->irq < 0) {
465                 dev_err(dev, "missing IRQ\n");
466                 return -ENODEV;
467         }
468
469         INIT_WORK(&ci->work, ci_role_work);
470         ci->wq = create_singlethread_workqueue("ci_otg");
471         if (!ci->wq) {
472                 dev_err(dev, "can't create workqueue\n");
473                 return -ENODEV;
474         }
475
476         if (!ci->platdata->phy_mode)
477                 ci->platdata->phy_mode = of_usb_get_phy_mode(of_node);
478
479         hw_phymode_configure(ci);
480
481         if (!ci->platdata->dr_mode)
482                 ci->platdata->dr_mode = of_usb_get_dr_mode(of_node);
483
484         if (ci->platdata->dr_mode == USB_DR_MODE_UNKNOWN)
485                 ci->platdata->dr_mode = USB_DR_MODE_OTG;
486
487         dr_mode = ci->platdata->dr_mode;
488         /* initialize role(s) before the interrupt is requested */
489         if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) {
490                 ret = ci_hdrc_host_init(ci);
491                 if (ret)
492                         dev_info(dev, "doesn't support host\n");
493         }
494
495         if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) {
496                 ret = ci_hdrc_gadget_init(ci);
497                 if (ret)
498                         dev_info(dev, "doesn't support gadget\n");
499         }
500
501         if (!ci->roles[CI_ROLE_HOST] && !ci->roles[CI_ROLE_GADGET]) {
502                 dev_err(dev, "no supported roles\n");
503                 ret = -ENODEV;
504                 goto rm_wq;
505         }
506
507         if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET]) {
508                 ci->is_otg = true;
509                 /* ID pin needs 1ms debouce time, we delay 2ms for safe */
510                 mdelay(2);
511                 ci->role = ci_otg_role(ci);
512         } else {
513                 ci->role = ci->roles[CI_ROLE_HOST]
514                         ? CI_ROLE_HOST
515                         : CI_ROLE_GADGET;
516         }
517
518         ret = ci_role_start(ci, ci->role);
519         if (ret) {
520                 dev_err(dev, "can't start %s role\n", ci_role(ci)->name);
521                 goto rm_wq;
522         }
523
524         platform_set_drvdata(pdev, ci);
525         ret = request_irq(ci->irq, ci_irq, IRQF_SHARED, ci->platdata->name,
526                           ci);
527         if (ret)
528                 goto stop;
529
530         if (ci->is_otg)
531                 hw_write(ci, OP_OTGSC, OTGSC_IDIE, OTGSC_IDIE);
532
533         ret = dbg_create_files(ci);
534         if (!ret)
535                 return 0;
536
537         free_irq(ci->irq, ci);
538 stop:
539         ci_role_stop(ci);
540 rm_wq:
541         flush_workqueue(ci->wq);
542         destroy_workqueue(ci->wq);
543
544         return ret;
545 }
546
547 static int ci_hdrc_remove(struct platform_device *pdev)
548 {
549         struct ci_hdrc *ci = platform_get_drvdata(pdev);
550
551         dbg_remove_files(ci);
552         flush_workqueue(ci->wq);
553         destroy_workqueue(ci->wq);
554         free_irq(ci->irq, ci);
555         ci_role_stop(ci);
556
557         return 0;
558 }
559
560 static struct platform_driver ci_hdrc_driver = {
561         .probe  = ci_hdrc_probe,
562         .remove = ci_hdrc_remove,
563         .driver = {
564                 .name   = "ci_hdrc",
565         },
566 };
567
568 module_platform_driver(ci_hdrc_driver);
569
570 MODULE_ALIAS("platform:ci_hdrc");
571 MODULE_LICENSE("GPL v2");
572 MODULE_AUTHOR("David Lopo <dlopo@chipidea.mips.com>");
573 MODULE_DESCRIPTION("ChipIdea HDRC Driver");