inet: frag: enforce memory limits earlier
[linux/fpc-iii.git] / drivers / usb / host / ohci-omap.c
blob495c1454b9e81e229efc86086320375a1c3dee6f
1 /*
2 * OHCI HCD (Host Controller Driver) for USB.
4 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5 * (C) Copyright 2000-2005 David Brownell
6 * (C) Copyright 2002 Hewlett-Packard Company
8 * OMAP Bus Glue
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>
14 * This file is licenced under the GPL.
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>
31 #include "ohci.h"
33 #include <asm/io.h>
34 #include <asm/mach-types.h>
36 #include <mach/mux.h>
38 #include <mach/hardware.h>
39 #include <mach/usb.h>
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
53 #define DRIVER_DESC "OHCI OMAP driver"
55 #ifdef CONFIG_TPS65010
56 #include <linux/i2c/tps65010.h>
57 #else
59 #define LOW 0
60 #define HIGH 1
62 #define GPIO1 1
64 static inline int tps65010_set_gpio_out_value(unsigned gpio, unsigned value)
66 return 0;
69 #endif
71 extern int usb_disabled(void);
72 extern int ocpi_enable(void);
74 static struct clk *usb_host_ck;
75 static struct clk *usb_dc_ck;
77 static const char hcd_name[] = "ohci-omap";
78 static struct hc_driver __read_mostly ohci_omap_hc_driver;
80 static void omap_ohci_clock_power(int on)
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);
94 * Board specific gang-switched transceiver power on/off.
95 * NOTE: OSK supplies power from DC, not battery.
97 static int omap_ohci_transceiver_power(int on)
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);
115 return 0;
118 #ifdef CONFIG_ARCH_OMAP15XX
120 * OMAP-1510 specific Local Bus clock on/off
122 static int omap_1510_local_bus_power(int on)
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);
131 return 0;
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.
141 static int omap_1510_local_bus_init(void)
143 unsigned int tlb;
144 unsigned long lbaddr, physaddr;
146 omap_writel((omap_readl(OMAP1510_LB_CLOCK_DIV) & 0xfffffff8) | 0x4,
147 OMAP1510_LB_CLOCK_DIV);
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);
162 /* Enable the walking table */
163 omap_writel(omap_readl(OMAP1510_LB_MMU_CTL) | (1 << 3), OMAP1510_LB_MMU_CTL);
164 udelay(200);
166 return 0;
168 #else
169 #define omap_1510_local_bus_power(x) {}
170 #define omap_1510_local_bus_init() {}
171 #endif
173 #ifdef CONFIG_USB_OTG
175 static void start_hnp(struct ohci_hcd *ohci)
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;
182 otg_start_hnp(hcd->usb_phy->otg);
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);
193 #endif
195 /*-------------------------------------------------------------------------*/
197 static int ohci_omap_reset(struct usb_hcd *hcd)
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;
204 dev_dbg(hcd->self.controller, "starting USB Controller\n");
206 if (config->otg) {
207 hcd->self.otg_port = config->otg;
208 /* default/minimum OTG power budget: 8 mA */
209 hcd->power_budget = 8;
212 /* boards can use OTG transceivers in non-OTG modes */
213 need_transceiver = need_transceiver
214 || machine_is_omap_h2() || machine_is_omap_h3();
216 /* XXX OMAP16xx only */
217 if (config->ocpi_enable)
218 config->ocpi_enable();
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;
232 } else {
233 dev_err(hcd->self.controller, "can't find phy\n");
234 return -ENODEV;
236 ohci->start_hnp = start_hnp;
238 #endif
240 omap_ohci_clock_power(1);
242 if (cpu_is_omap15xx()) {
243 omap_1510_local_bus_power(1);
244 omap_1510_local_bus_init();
247 ret = ohci_setup(hcd);
248 if (ret < 0)
249 return ret;
251 if (config->otg || config->rwc) {
252 ohci->hc_control = OHCI_CTRL_RWC;
253 writel(OHCI_CTRL_RWC, &ohci->regs->control);
256 /* board-specific power switching and overcurrent support */
257 if (machine_is_omap_osk() || machine_is_omap_innovator()) {
258 u32 rh = roothub_a (ohci);
260 /* power switching (ganged by default) */
261 rh &= ~RH_A_NPS;
263 /* TPS2045 switch for internal transceiver (port 1) */
264 if (machine_is_omap_osk()) {
265 ohci_to_hcd(ohci)->power_budget = 250;
267 rh &= ~RH_A_NOCP;
269 /* gpio9 for overcurrent detction */
270 omap_cfg_reg(W8_1610_GPIO9);
271 gpio_request(9, "OHCI overcurrent");
272 gpio_direction_input(9);
274 /* for paranoia's sake: disable USB.PUEN */
275 omap_cfg_reg(W4_USB_HIGHZ);
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;
285 /* FIXME hub_wq hub requests should manage power switching */
286 omap_ohci_transceiver_power(1);
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.
293 return 0;
296 /*-------------------------------------------------------------------------*/
299 * usb_hcd_omap_probe - initialize OMAP-based HCDs
300 * Context: !in_interrupt()
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.
306 static int usb_hcd_omap_probe (const struct hc_driver *driver,
307 struct platform_device *pdev)
309 int retval, irq;
310 struct usb_hcd *hcd = 0;
312 if (pdev->num_resources != 2) {
313 dev_err(&pdev->dev, "invalid num_resources: %i\n",
314 pdev->num_resources);
315 return -ENODEV;
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;
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);
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");
333 if (IS_ERR(usb_dc_ck)) {
334 clk_put(usb_host_ck);
335 return PTR_ERR(usb_dc_ck);
339 hcd = usb_create_hcd (driver, &pdev->dev, dev_name(&pdev->dev));
340 if (!hcd) {
341 retval = -ENOMEM;
342 goto err0;
344 hcd->rsrc_start = pdev->resource[0].start;
345 hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;
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;
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;
360 irq = platform_get_irq(pdev, 0);
361 if (irq < 0) {
362 retval = -ENXIO;
363 goto err3;
365 retval = usb_add_hcd(hcd, irq, 0);
366 if (retval)
367 goto err3;
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;
384 /* may be called with controller, bus, and devices active */
387 * usb_hcd_omap_remove - shutdown processing for OMAP-based HCDs
388 * @dev: USB Host Controller being removed
389 * Context: !in_interrupt()
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.
395 static inline void
396 usb_hcd_omap_remove (struct usb_hcd *hcd, struct platform_device *pdev)
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);
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);
414 /*-------------------------------------------------------------------------*/
416 static int ohci_hcd_omap_drv_probe(struct platform_device *dev)
418 return usb_hcd_omap_probe(&ohci_omap_hc_driver, dev);
421 static int ohci_hcd_omap_drv_remove(struct platform_device *dev)
423 struct usb_hcd *hcd = platform_get_drvdata(dev);
425 usb_hcd_omap_remove(hcd, dev);
427 return 0;
430 /*-------------------------------------------------------------------------*/
432 #ifdef CONFIG_PM
434 static int ohci_omap_suspend(struct platform_device *pdev, pm_message_t message)
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;
441 if (time_before(jiffies, ohci->next_statechange))
442 msleep(5);
443 ohci->next_statechange = jiffies;
445 ret = ohci_suspend(hcd, do_wakeup);
446 if (ret)
447 return ret;
449 omap_ohci_clock_power(0);
450 return ret;
453 static int ohci_omap_resume(struct platform_device *dev)
455 struct usb_hcd *hcd = platform_get_drvdata(dev);
456 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
458 if (time_before(jiffies, ohci->next_statechange))
459 msleep(5);
460 ohci->next_statechange = jiffies;
462 omap_ohci_clock_power(1);
463 ohci_resume(hcd, false);
464 return 0;
467 #endif
469 /*-------------------------------------------------------------------------*/
472 * Driver definition to register with the OMAP bus
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",
487 static const struct ohci_driver_overrides omap_overrides __initconst = {
488 .product_desc = "OMAP OHCI",
489 .reset = ohci_omap_reset
492 static int __init ohci_omap_init(void)
494 if (usb_disabled())
495 return -ENODEV;
497 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
499 ohci_init_driver(&ohci_omap_hc_driver, &omap_overrides);
500 return platform_driver_register(&ohci_hcd_omap_driver);
502 module_init(ohci_omap_init);
504 static void __exit ohci_omap_cleanup(void)
506 platform_driver_unregister(&ohci_hcd_omap_driver);
508 module_exit(ohci_omap_cleanup);
510 MODULE_DESCRIPTION(DRIVER_DESC);
511 MODULE_ALIAS("platform:ohci");
512 MODULE_LICENSE("GPL");