]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/usb/host/ohci-omap.c
1e809d7c9ff82e07ec2422ac929368e64083158f
[mirror_ubuntu-bionic-kernel.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 * ohci_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 ohci_hcd_omap_probe(struct platform_device *pdev)
307 {
308 int retval, irq;
309 struct usb_hcd *hcd = 0;
310
311 if (pdev->num_resources != 2) {
312 dev_err(&pdev->dev, "invalid num_resources: %i\n",
313 pdev->num_resources);
314 return -ENODEV;
315 }
316
317 if (pdev->resource[0].flags != IORESOURCE_MEM
318 || pdev->resource[1].flags != IORESOURCE_IRQ) {
319 dev_err(&pdev->dev, "invalid resource type\n");
320 return -ENODEV;
321 }
322
323 usb_host_ck = clk_get(&pdev->dev, "usb_hhc_ck");
324 if (IS_ERR(usb_host_ck))
325 return PTR_ERR(usb_host_ck);
326
327 if (!cpu_is_omap15xx())
328 usb_dc_ck = clk_get(&pdev->dev, "usb_dc_ck");
329 else
330 usb_dc_ck = clk_get(&pdev->dev, "lb_ck");
331
332 if (IS_ERR(usb_dc_ck)) {
333 clk_put(usb_host_ck);
334 return PTR_ERR(usb_dc_ck);
335 }
336
337
338 hcd = usb_create_hcd(&ohci_omap_hc_driver, &pdev->dev,
339 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 * ohci_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 ohci_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 int ohci_hcd_omap_remove(struct platform_device *pdev)
396 {
397 struct usb_hcd *hcd = platform_get_drvdata(pdev);
398
399 dev_dbg(hcd->self.controller, "stopping USB Controller\n");
400 usb_remove_hcd(hcd);
401 omap_ohci_clock_power(0);
402 if (!IS_ERR_OR_NULL(hcd->usb_phy)) {
403 (void) otg_set_host(hcd->usb_phy->otg, 0);
404 usb_put_phy(hcd->usb_phy);
405 }
406 if (machine_is_omap_osk())
407 gpio_free(9);
408 iounmap(hcd->regs);
409 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
410 usb_put_hcd(hcd);
411 clk_put(usb_dc_ck);
412 clk_put(usb_host_ck);
413 return 0;
414 }
415
416 /*-------------------------------------------------------------------------*/
417
418 #ifdef CONFIG_PM
419
420 static int ohci_omap_suspend(struct platform_device *pdev, pm_message_t message)
421 {
422 struct usb_hcd *hcd = platform_get_drvdata(pdev);
423 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
424 bool do_wakeup = device_may_wakeup(&pdev->dev);
425 int ret;
426
427 if (time_before(jiffies, ohci->next_statechange))
428 msleep(5);
429 ohci->next_statechange = jiffies;
430
431 ret = ohci_suspend(hcd, do_wakeup);
432 if (ret)
433 return ret;
434
435 omap_ohci_clock_power(0);
436 return ret;
437 }
438
439 static int ohci_omap_resume(struct platform_device *dev)
440 {
441 struct usb_hcd *hcd = platform_get_drvdata(dev);
442 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
443
444 if (time_before(jiffies, ohci->next_statechange))
445 msleep(5);
446 ohci->next_statechange = jiffies;
447
448 omap_ohci_clock_power(1);
449 ohci_resume(hcd, false);
450 return 0;
451 }
452
453 #endif
454
455 /*-------------------------------------------------------------------------*/
456
457 /*
458 * Driver definition to register with the OMAP bus
459 */
460 static struct platform_driver ohci_hcd_omap_driver = {
461 .probe = ohci_hcd_omap_probe,
462 .remove = ohci_hcd_omap_remove,
463 .shutdown = usb_hcd_platform_shutdown,
464 #ifdef CONFIG_PM
465 .suspend = ohci_omap_suspend,
466 .resume = ohci_omap_resume,
467 #endif
468 .driver = {
469 .name = "ohci",
470 },
471 };
472
473 static const struct ohci_driver_overrides omap_overrides __initconst = {
474 .product_desc = "OMAP OHCI",
475 .reset = ohci_omap_reset
476 };
477
478 static int __init ohci_omap_init(void)
479 {
480 if (usb_disabled())
481 return -ENODEV;
482
483 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
484
485 ohci_init_driver(&ohci_omap_hc_driver, &omap_overrides);
486 return platform_driver_register(&ohci_hcd_omap_driver);
487 }
488 module_init(ohci_omap_init);
489
490 static void __exit ohci_omap_cleanup(void)
491 {
492 platform_driver_unregister(&ohci_hcd_omap_driver);
493 }
494 module_exit(ohci_omap_cleanup);
495
496 MODULE_DESCRIPTION(DRIVER_DESC);
497 MODULE_ALIAS("platform:ohci");
498 MODULE_LICENSE("GPL");