Merge tag 'iommu-updates-v4.9' of git://git.kernel.org/pub/scm/linux/kernel/git/joro...
[cascardo/linux.git] / drivers / usb / host / ohci-omap.c
1 /*
2  * OHCI HCD (Host Controller Driver) for USB.
3  *
4  * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5  * (C) Copyright 2000-2005 David Brownell
6  * (C) Copyright 2002 Hewlett-Packard Company
7  *
8  * OMAP Bus Glue
9  *
10  * Modified for OMAP by Tony Lindgren <tony@atomide.com>
11  * Based on the 2.4 OMAP OHCI driver originally done by MontaVista Software Inc.
12  * and on ohci-sa1111.c by Christopher Hoover <ch@hpl.hp.com>
13  *
14  * This file is licenced under the GPL.
15  */
16
17 #include <linux/clk.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/err.h>
20 #include <linux/gpio.h>
21 #include <linux/io.h>
22 #include <linux/jiffies.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/usb/otg.h>
26 #include <linux/platform_device.h>
27 #include <linux/signal.h>
28 #include <linux/usb.h>
29 #include <linux/usb/hcd.h>
30
31 #include "ohci.h"
32
33 #include <asm/io.h>
34 #include <asm/mach-types.h>
35
36 #include <mach/mux.h>
37
38 #include <mach/hardware.h>
39 #include <mach/usb.h>
40
41
42 /* OMAP-1510 OHCI has its own MMU for DMA */
43 #define OMAP1510_LB_MEMSIZE     32      /* Should be same as SDRAM size */
44 #define OMAP1510_LB_CLOCK_DIV   0xfffec10c
45 #define OMAP1510_LB_MMU_CTL     0xfffec208
46 #define OMAP1510_LB_MMU_LCK     0xfffec224
47 #define OMAP1510_LB_MMU_LD_TLB  0xfffec228
48 #define OMAP1510_LB_MMU_CAM_H   0xfffec22c
49 #define OMAP1510_LB_MMU_CAM_L   0xfffec230
50 #define OMAP1510_LB_MMU_RAM_H   0xfffec234
51 #define OMAP1510_LB_MMU_RAM_L   0xfffec238
52
53 #define DRIVER_DESC "OHCI OMAP driver"
54
55 #ifdef CONFIG_TPS65010
56 #include <linux/i2c/tps65010.h>
57 #else
58
59 #define LOW     0
60 #define HIGH    1
61
62 #define GPIO1   1
63
64 static inline int tps65010_set_gpio_out_value(unsigned gpio, unsigned value)
65 {
66         return 0;
67 }
68
69 #endif
70
71 extern int usb_disabled(void);
72 extern int ocpi_enable(void);
73
74 static struct clk *usb_host_ck;
75 static struct clk *usb_dc_ck;
76
77 static const char hcd_name[] = "ohci-omap";
78 static struct hc_driver __read_mostly ohci_omap_hc_driver;
79
80 static void omap_ohci_clock_power(int on)
81 {
82         if (on) {
83                 clk_enable(usb_dc_ck);
84                 clk_enable(usb_host_ck);
85                 /* guesstimate for T5 == 1x 32K clock + APLL lock time */
86                 udelay(100);
87         } else {
88                 clk_disable(usb_host_ck);
89                 clk_disable(usb_dc_ck);
90         }
91 }
92
93 /*
94  * Board specific gang-switched transceiver power on/off.
95  * NOTE:  OSK supplies power from DC, not battery.
96  */
97 static int omap_ohci_transceiver_power(int on)
98 {
99         if (on) {
100                 if (machine_is_omap_innovator() && cpu_is_omap1510())
101                         __raw_writeb(__raw_readb(INNOVATOR_FPGA_CAM_USB_CONTROL)
102                                 | ((1 << 5/*usb1*/) | (1 << 3/*usb2*/)),
103                                INNOVATOR_FPGA_CAM_USB_CONTROL);
104                 else if (machine_is_omap_osk())
105                         tps65010_set_gpio_out_value(GPIO1, LOW);
106         } else {
107                 if (machine_is_omap_innovator() && cpu_is_omap1510())
108                         __raw_writeb(__raw_readb(INNOVATOR_FPGA_CAM_USB_CONTROL)
109                                 & ~((1 << 5/*usb1*/) | (1 << 3/*usb2*/)),
110                                INNOVATOR_FPGA_CAM_USB_CONTROL);
111                 else if (machine_is_omap_osk())
112                         tps65010_set_gpio_out_value(GPIO1, HIGH);
113         }
114
115         return 0;
116 }
117
118 #ifdef CONFIG_ARCH_OMAP15XX
119 /*
120  * OMAP-1510 specific Local Bus clock on/off
121  */
122 static int omap_1510_local_bus_power(int on)
123 {
124         if (on) {
125                 omap_writel((1 << 1) | (1 << 0), OMAP1510_LB_MMU_CTL);
126                 udelay(200);
127         } else {
128                 omap_writel(0, OMAP1510_LB_MMU_CTL);
129         }
130
131         return 0;
132 }
133
134 /*
135  * OMAP-1510 specific Local Bus initialization
136  * NOTE: This assumes 32MB memory size in OMAP1510LB_MEMSIZE.
137  *       See also arch/mach-omap/memory.h for __virt_to_dma() and
138  *       __dma_to_virt() which need to match with the physical
139  *       Local Bus address below.
140  */
141 static int omap_1510_local_bus_init(void)
142 {
143         unsigned int tlb;
144         unsigned long lbaddr, physaddr;
145
146         omap_writel((omap_readl(OMAP1510_LB_CLOCK_DIV) & 0xfffffff8) | 0x4,
147                OMAP1510_LB_CLOCK_DIV);
148
149         /* Configure the Local Bus MMU table */
150         for (tlb = 0; tlb < OMAP1510_LB_MEMSIZE; tlb++) {
151                 lbaddr = tlb * 0x00100000 + OMAP1510_LB_OFFSET;
152                 physaddr = tlb * 0x00100000 + PHYS_OFFSET;
153                 omap_writel((lbaddr & 0x0fffffff) >> 22, OMAP1510_LB_MMU_CAM_H);
154                 omap_writel(((lbaddr & 0x003ffc00) >> 6) | 0xc,
155                        OMAP1510_LB_MMU_CAM_L);
156                 omap_writel(physaddr >> 16, OMAP1510_LB_MMU_RAM_H);
157                 omap_writel((physaddr & 0x0000fc00) | 0x300, OMAP1510_LB_MMU_RAM_L);
158                 omap_writel(tlb << 4, OMAP1510_LB_MMU_LCK);
159                 omap_writel(0x1, OMAP1510_LB_MMU_LD_TLB);
160         }
161
162         /* Enable the walking table */
163         omap_writel(omap_readl(OMAP1510_LB_MMU_CTL) | (1 << 3), OMAP1510_LB_MMU_CTL);
164         udelay(200);
165
166         return 0;
167 }
168 #else
169 #define omap_1510_local_bus_power(x)    {}
170 #define omap_1510_local_bus_init()      {}
171 #endif
172
173 #ifdef  CONFIG_USB_OTG
174
175 static void start_hnp(struct ohci_hcd *ohci)
176 {
177         struct usb_hcd *hcd = ohci_to_hcd(ohci);
178         const unsigned  port = hcd->self.otg_port - 1;
179         unsigned long   flags;
180         u32 l;
181
182         otg_start_hnp(hcd->usb_phy->otg);
183
184         local_irq_save(flags);
185         hcd->usb_phy->otg->state = OTG_STATE_A_SUSPEND;
186         writel (RH_PS_PSS, &ohci->regs->roothub.portstatus [port]);
187         l = omap_readl(OTG_CTRL);
188         l &= ~OTG_A_BUSREQ;
189         omap_writel(l, OTG_CTRL);
190         local_irq_restore(flags);
191 }
192
193 #endif
194
195 /*-------------------------------------------------------------------------*/
196
197 static int ohci_omap_reset(struct usb_hcd *hcd)
198 {
199         struct ohci_hcd         *ohci = hcd_to_ohci(hcd);
200         struct omap_usb_config  *config = dev_get_platdata(hcd->self.controller);
201         int                     need_transceiver = (config->otg != 0);
202         int                     ret;
203
204         dev_dbg(hcd->self.controller, "starting USB Controller\n");
205
206         if (config->otg) {
207                 hcd->self.otg_port = config->otg;
208                 /* default/minimum OTG power budget:  8 mA */
209                 hcd->power_budget = 8;
210         }
211
212         /* boards can use OTG transceivers in non-OTG modes */
213         need_transceiver = need_transceiver
214                         || machine_is_omap_h2() || machine_is_omap_h3();
215
216         /* XXX OMAP16xx only */
217         if (config->ocpi_enable)
218                 config->ocpi_enable();
219
220 #ifdef  CONFIG_USB_OTG
221         if (need_transceiver) {
222                 hcd->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
223                 if (!IS_ERR_OR_NULL(hcd->usb_phy)) {
224                         int     status = otg_set_host(hcd->usb_phy->otg,
225                                                 &ohci_to_hcd(ohci)->self);
226                         dev_dbg(hcd->self.controller, "init %s phy, status %d\n",
227                                         hcd->usb_phy->label, status);
228                         if (status) {
229                                 usb_put_phy(hcd->usb_phy);
230                                 return status;
231                         }
232                 } else {
233                         dev_err(hcd->self.controller, "can't find phy\n");
234                         return -ENODEV;
235                 }
236                 ohci->start_hnp = start_hnp;
237         }
238 #endif
239
240         omap_ohci_clock_power(1);
241
242         if (cpu_is_omap15xx()) {
243                 omap_1510_local_bus_power(1);
244                 omap_1510_local_bus_init();
245         }
246
247         ret = ohci_setup(hcd);
248         if (ret < 0)
249                 return ret;
250
251         if (config->otg || config->rwc) {
252                 ohci->hc_control = OHCI_CTRL_RWC;
253                 writel(OHCI_CTRL_RWC, &ohci->regs->control);
254         }
255
256         /* board-specific power switching and overcurrent support */
257         if (machine_is_omap_osk() || machine_is_omap_innovator()) {
258                 u32     rh = roothub_a (ohci);
259
260                 /* power switching (ganged by default) */
261                 rh &= ~RH_A_NPS;
262
263                 /* TPS2045 switch for internal transceiver (port 1) */
264                 if (machine_is_omap_osk()) {
265                         ohci_to_hcd(ohci)->power_budget = 250;
266
267                         rh &= ~RH_A_NOCP;
268
269                         /* gpio9 for overcurrent detction */
270                         omap_cfg_reg(W8_1610_GPIO9);
271                         gpio_request(9, "OHCI overcurrent");
272                         gpio_direction_input(9);
273
274                         /* for paranoia's sake:  disable USB.PUEN */
275                         omap_cfg_reg(W4_USB_HIGHZ);
276                 }
277                 ohci_writel(ohci, rh, &ohci->regs->roothub.a);
278                 ohci->flags &= ~OHCI_QUIRK_HUB_POWER;
279         } else if (machine_is_nokia770()) {
280                 /* We require a self-powered hub, which should have
281                  * plenty of power. */
282                 ohci_to_hcd(ohci)->power_budget = 0;
283         }
284
285         /* FIXME hub_wq hub requests should manage power switching */
286         omap_ohci_transceiver_power(1);
287
288         /* board init will have already handled HMC and mux setup.
289          * any external transceiver should already be initialized
290          * too, so all configured ports use the right signaling now.
291          */
292
293         return 0;
294 }
295
296 /*-------------------------------------------------------------------------*/
297
298 /**
299  * usb_hcd_omap_probe - initialize OMAP-based HCDs
300  * Context: !in_interrupt()
301  *
302  * Allocates basic resources for this USB host controller, and
303  * then invokes the start() method for the HCD associated with it
304  * through the hotplug entry's driver_data.
305  */
306 static int usb_hcd_omap_probe (const struct hc_driver *driver,
307                           struct platform_device *pdev)
308 {
309         int retval, irq;
310         struct usb_hcd *hcd = 0;
311
312         if (pdev->num_resources != 2) {
313                 dev_err(&pdev->dev, "invalid num_resources: %i\n",
314                        pdev->num_resources);
315                 return -ENODEV;
316         }
317
318         if (pdev->resource[0].flags != IORESOURCE_MEM
319                         || pdev->resource[1].flags != IORESOURCE_IRQ) {
320                 dev_err(&pdev->dev, "invalid resource type\n");
321                 return -ENODEV;
322         }
323
324         usb_host_ck = clk_get(&pdev->dev, "usb_hhc_ck");
325         if (IS_ERR(usb_host_ck))
326                 return PTR_ERR(usb_host_ck);
327
328         if (!cpu_is_omap15xx())
329                 usb_dc_ck = clk_get(&pdev->dev, "usb_dc_ck");
330         else
331                 usb_dc_ck = clk_get(&pdev->dev, "lb_ck");
332
333         if (IS_ERR(usb_dc_ck)) {
334                 clk_put(usb_host_ck);
335                 return PTR_ERR(usb_dc_ck);
336         }
337
338
339         hcd = usb_create_hcd (driver, &pdev->dev, dev_name(&pdev->dev));
340         if (!hcd) {
341                 retval = -ENOMEM;
342                 goto err0;
343         }
344         hcd->rsrc_start = pdev->resource[0].start;
345         hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;
346
347         if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
348                 dev_dbg(&pdev->dev, "request_mem_region failed\n");
349                 retval = -EBUSY;
350                 goto err1;
351         }
352
353         hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
354         if (!hcd->regs) {
355                 dev_err(&pdev->dev, "can't ioremap OHCI HCD\n");
356                 retval = -ENOMEM;
357                 goto err2;
358         }
359
360         irq = platform_get_irq(pdev, 0);
361         if (irq < 0) {
362                 retval = -ENXIO;
363                 goto err3;
364         }
365         retval = usb_add_hcd(hcd, irq, 0);
366         if (retval)
367                 goto err3;
368
369         device_wakeup_enable(hcd->self.controller);
370         return 0;
371 err3:
372         iounmap(hcd->regs);
373 err2:
374         release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
375 err1:
376         usb_put_hcd(hcd);
377 err0:
378         clk_put(usb_dc_ck);
379         clk_put(usb_host_ck);
380         return retval;
381 }
382
383
384 /* may be called with controller, bus, and devices active */
385
386 /**
387  * usb_hcd_omap_remove - shutdown processing for OMAP-based HCDs
388  * @dev: USB Host Controller being removed
389  * Context: !in_interrupt()
390  *
391  * Reverses the effect of usb_hcd_omap_probe(), first invoking
392  * the HCD's stop() method.  It is always called from a thread
393  * context, normally "rmmod", "apmd", or something similar.
394  */
395 static inline void
396 usb_hcd_omap_remove (struct usb_hcd *hcd, struct platform_device *pdev)
397 {
398         dev_dbg(hcd->self.controller, "stopping USB Controller\n");
399         usb_remove_hcd(hcd);
400         omap_ohci_clock_power(0);
401         if (!IS_ERR_OR_NULL(hcd->usb_phy)) {
402                 (void) otg_set_host(hcd->usb_phy->otg, 0);
403                 usb_put_phy(hcd->usb_phy);
404         }
405         if (machine_is_omap_osk())
406                 gpio_free(9);
407         iounmap(hcd->regs);
408         release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
409         usb_put_hcd(hcd);
410         clk_put(usb_dc_ck);
411         clk_put(usb_host_ck);
412 }
413
414 /*-------------------------------------------------------------------------*/
415
416 static int ohci_hcd_omap_drv_probe(struct platform_device *dev)
417 {
418         return usb_hcd_omap_probe(&ohci_omap_hc_driver, dev);
419 }
420
421 static int ohci_hcd_omap_drv_remove(struct platform_device *dev)
422 {
423         struct usb_hcd          *hcd = platform_get_drvdata(dev);
424
425         usb_hcd_omap_remove(hcd, dev);
426
427         return 0;
428 }
429
430 /*-------------------------------------------------------------------------*/
431
432 #ifdef  CONFIG_PM
433
434 static int ohci_omap_suspend(struct platform_device *pdev, pm_message_t message)
435 {
436         struct usb_hcd *hcd = platform_get_drvdata(pdev);
437         struct ohci_hcd *ohci = hcd_to_ohci(hcd);
438         bool do_wakeup = device_may_wakeup(&pdev->dev);
439         int ret;
440
441         if (time_before(jiffies, ohci->next_statechange))
442                 msleep(5);
443         ohci->next_statechange = jiffies;
444
445         ret = ohci_suspend(hcd, do_wakeup);
446         if (ret)
447                 return ret;
448
449         omap_ohci_clock_power(0);
450         return ret;
451 }
452
453 static int ohci_omap_resume(struct platform_device *dev)
454 {
455         struct usb_hcd  *hcd = platform_get_drvdata(dev);
456         struct ohci_hcd *ohci = hcd_to_ohci(hcd);
457
458         if (time_before(jiffies, ohci->next_statechange))
459                 msleep(5);
460         ohci->next_statechange = jiffies;
461
462         omap_ohci_clock_power(1);
463         ohci_resume(hcd, false);
464         return 0;
465 }
466
467 #endif
468
469 /*-------------------------------------------------------------------------*/
470
471 /*
472  * Driver definition to register with the OMAP bus
473  */
474 static struct platform_driver ohci_hcd_omap_driver = {
475         .probe          = ohci_hcd_omap_drv_probe,
476         .remove         = ohci_hcd_omap_drv_remove,
477         .shutdown       = usb_hcd_platform_shutdown,
478 #ifdef  CONFIG_PM
479         .suspend        = ohci_omap_suspend,
480         .resume         = ohci_omap_resume,
481 #endif
482         .driver         = {
483                 .name   = "ohci",
484         },
485 };
486
487 static const struct ohci_driver_overrides omap_overrides __initconst = {
488         .product_desc   = "OMAP OHCI",
489         .reset          = ohci_omap_reset
490 };
491
492 static int __init ohci_omap_init(void)
493 {
494         if (usb_disabled())
495                 return -ENODEV;
496
497         pr_info("%s: " DRIVER_DESC "\n", hcd_name);
498
499         ohci_init_driver(&ohci_omap_hc_driver, &omap_overrides);
500         return platform_driver_register(&ohci_hcd_omap_driver);
501 }
502 module_init(ohci_omap_init);
503
504 static void __exit ohci_omap_cleanup(void)
505 {
506         platform_driver_unregister(&ohci_hcd_omap_driver);
507 }
508 module_exit(ohci_omap_cleanup);
509
510 MODULE_DESCRIPTION(DRIVER_DESC);
511 MODULE_ALIAS("platform:ohci");
512 MODULE_LICENSE("GPL");