Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * OHCI HCD (Host Controller Driver) for USB. |
| 3 | * |
| 4 | * Copyright (C) 2004 SAN People (Pty) Ltd. |
| 5 | * Copyright (C) 2005 Thibaut VARENE <varenet@parisc-linux.org> |
| 6 | * |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 7 | * AT91 Bus Glue |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 8 | * |
| 9 | * Based on fragments of 2.4 driver by Rick Bronson. |
| 10 | * Based on ohci-omap.c |
| 11 | * |
| 12 | * This file is licenced under the GPL. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/clk.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | |
| 18 | #include <asm/mach-types.h> |
| 19 | #include <asm/hardware.h> |
| 20 | #include <asm/arch/board.h> |
Andrew Victor | ed077bb | 2007-02-16 10:18:58 -0800 | [diff] [blame^] | 21 | #include <asm/arch/cpu.h> |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 22 | |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 23 | #ifndef CONFIG_ARCH_AT91 |
| 24 | #error "CONFIG_ARCH_AT91 must be defined." |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 25 | #endif |
| 26 | |
Andrew Victor | ed077bb | 2007-02-16 10:18:58 -0800 | [diff] [blame^] | 27 | /* interface and function clocks; sometimes also an AHB clock */ |
| 28 | static struct clk *iclk, *fclk, *hclk; |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 29 | static int clocked; |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 30 | |
| 31 | extern int usb_disabled(void); |
| 32 | |
| 33 | /*-------------------------------------------------------------------------*/ |
| 34 | |
Andrew Victor | ed077bb | 2007-02-16 10:18:58 -0800 | [diff] [blame^] | 35 | static void at91_start_clock(void) |
| 36 | { |
| 37 | if (cpu_is_at91sam9261()) |
| 38 | clk_enable(hclk); |
| 39 | clk_enable(iclk); |
| 40 | clk_enable(fclk); |
| 41 | clocked = 1; |
| 42 | } |
| 43 | |
| 44 | static void at91_stop_clock(void) |
| 45 | { |
| 46 | clk_disable(fclk); |
| 47 | clk_disable(iclk); |
| 48 | if (cpu_is_at91sam9261()) |
| 49 | clk_disable(hclk); |
| 50 | clocked = 0; |
| 51 | } |
| 52 | |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 53 | static void at91_start_hc(struct platform_device *pdev) |
| 54 | { |
| 55 | struct usb_hcd *hcd = platform_get_drvdata(pdev); |
| 56 | struct ohci_regs __iomem *regs = hcd->regs; |
| 57 | |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 58 | dev_dbg(&pdev->dev, "start\n"); |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 59 | |
| 60 | /* |
| 61 | * Start the USB clocks. |
| 62 | */ |
Andrew Victor | ed077bb | 2007-02-16 10:18:58 -0800 | [diff] [blame^] | 63 | at91_start_clock(); |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * The USB host controller must remain in reset. |
| 67 | */ |
| 68 | writel(0, ®s->control); |
| 69 | } |
| 70 | |
| 71 | static void at91_stop_hc(struct platform_device *pdev) |
| 72 | { |
| 73 | struct usb_hcd *hcd = platform_get_drvdata(pdev); |
| 74 | struct ohci_regs __iomem *regs = hcd->regs; |
| 75 | |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 76 | dev_dbg(&pdev->dev, "stop\n"); |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 77 | |
| 78 | /* |
| 79 | * Put the USB host controller into reset. |
| 80 | */ |
| 81 | writel(0, ®s->control); |
| 82 | |
| 83 | /* |
| 84 | * Stop the USB clocks. |
| 85 | */ |
Andrew Victor | ed077bb | 2007-02-16 10:18:58 -0800 | [diff] [blame^] | 86 | at91_stop_clock(); |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | |
| 90 | /*-------------------------------------------------------------------------*/ |
| 91 | |
| 92 | static int usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *); |
| 93 | |
| 94 | /* configure so an HC device and id are always provided */ |
| 95 | /* always called with process context; sleeping is OK */ |
| 96 | |
| 97 | |
| 98 | /** |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 99 | * usb_hcd_at91_probe - initialize AT91-based HCDs |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 100 | * Context: !in_interrupt() |
| 101 | * |
| 102 | * Allocates basic resources for this USB host controller, and |
| 103 | * then invokes the start() method for the HCD associated with it |
| 104 | * through the hotplug entry's driver_data. |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 105 | */ |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 106 | static int usb_hcd_at91_probe(const struct hc_driver *driver, |
| 107 | struct platform_device *pdev) |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 108 | { |
| 109 | int retval; |
| 110 | struct usb_hcd *hcd = NULL; |
| 111 | |
| 112 | if (pdev->num_resources != 2) { |
| 113 | pr_debug("hcd probe: invalid num_resources"); |
| 114 | return -ENODEV; |
| 115 | } |
| 116 | |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 117 | if ((pdev->resource[0].flags != IORESOURCE_MEM) |
| 118 | || (pdev->resource[1].flags != IORESOURCE_IRQ)) { |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 119 | pr_debug("hcd probe: invalid resource type\n"); |
| 120 | return -ENODEV; |
| 121 | } |
| 122 | |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 123 | hcd = usb_create_hcd(driver, &pdev->dev, "at91"); |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 124 | if (!hcd) |
| 125 | return -ENOMEM; |
| 126 | hcd->rsrc_start = pdev->resource[0].start; |
| 127 | hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1; |
| 128 | |
| 129 | if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) { |
| 130 | pr_debug("request_mem_region failed\n"); |
| 131 | retval = -EBUSY; |
| 132 | goto err1; |
| 133 | } |
| 134 | |
| 135 | hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len); |
| 136 | if (!hcd->regs) { |
| 137 | pr_debug("ioremap failed\n"); |
| 138 | retval = -EIO; |
| 139 | goto err2; |
| 140 | } |
| 141 | |
| 142 | iclk = clk_get(&pdev->dev, "ohci_clk"); |
| 143 | fclk = clk_get(&pdev->dev, "uhpck"); |
Andrew Victor | ed077bb | 2007-02-16 10:18:58 -0800 | [diff] [blame^] | 144 | if (cpu_is_at91sam9261()) |
| 145 | hclk = clk_get(&pdev->dev, "hck0"); |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 146 | |
| 147 | at91_start_hc(pdev); |
| 148 | ohci_hcd_init(hcd_to_ohci(hcd)); |
| 149 | |
Thomas Gleixner | d54b5ca | 2006-07-01 19:29:44 -0700 | [diff] [blame] | 150 | retval = usb_add_hcd(hcd, pdev->resource[1].start, IRQF_DISABLED); |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 151 | if (retval == 0) |
| 152 | return retval; |
| 153 | |
| 154 | /* Error handling */ |
| 155 | at91_stop_hc(pdev); |
| 156 | |
Andrew Victor | ed077bb | 2007-02-16 10:18:58 -0800 | [diff] [blame^] | 157 | if (cpu_is_at91sam9261()) |
| 158 | clk_put(hclk); |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 159 | clk_put(fclk); |
| 160 | clk_put(iclk); |
| 161 | |
| 162 | iounmap(hcd->regs); |
| 163 | |
| 164 | err2: |
| 165 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); |
| 166 | |
| 167 | err1: |
| 168 | usb_put_hcd(hcd); |
| 169 | return retval; |
| 170 | } |
| 171 | |
| 172 | |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 173 | /* may be called with controller, bus, and devices active */ |
| 174 | |
| 175 | /** |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 176 | * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 177 | * @dev: USB Host Controller being removed |
| 178 | * Context: !in_interrupt() |
| 179 | * |
| 180 | * Reverses the effect of usb_hcd_at91_probe(), first invoking |
| 181 | * the HCD's stop() method. It is always called from a thread |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 182 | * context, "rmmod" or something similar. |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 183 | * |
| 184 | */ |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 185 | static int usb_hcd_at91_remove(struct usb_hcd *hcd, |
| 186 | struct platform_device *pdev) |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 187 | { |
| 188 | usb_remove_hcd(hcd); |
| 189 | at91_stop_hc(pdev); |
| 190 | iounmap(hcd->regs); |
David Brownell | 68ba61b | 2006-04-02 20:26:21 -0800 | [diff] [blame] | 191 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 192 | |
Andrew Victor | ed077bb | 2007-02-16 10:18:58 -0800 | [diff] [blame^] | 193 | if (cpu_is_at91sam9261()) |
| 194 | clk_put(hclk); |
David Brownell | 68ba61b | 2006-04-02 20:26:21 -0800 | [diff] [blame] | 195 | clk_put(fclk); |
| 196 | clk_put(iclk); |
Andrew Victor | ed077bb | 2007-02-16 10:18:58 -0800 | [diff] [blame^] | 197 | fclk = iclk = hclk = NULL; |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 198 | |
| 199 | dev_set_drvdata(&pdev->dev, NULL); |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | /*-------------------------------------------------------------------------*/ |
| 204 | |
| 205 | static int __devinit |
| 206 | ohci_at91_start (struct usb_hcd *hcd) |
| 207 | { |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 208 | struct at91_usbh_data *board = hcd->self.controller->platform_data; |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 209 | struct ohci_hcd *ohci = hcd_to_ohci (hcd); |
| 210 | int ret; |
| 211 | |
| 212 | if ((ret = ohci_init(ohci)) < 0) |
| 213 | return ret; |
| 214 | |
David Brownell | cd22afd | 2006-09-03 12:21:50 -0700 | [diff] [blame] | 215 | ohci->num_ports = board->ports; |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 216 | |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 217 | if ((ret = ohci_run(ohci)) < 0) { |
| 218 | err("can't start %s", hcd->self.bus_name); |
| 219 | ohci_stop(hcd); |
| 220 | return ret; |
| 221 | } |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | /*-------------------------------------------------------------------------*/ |
| 226 | |
| 227 | static const struct hc_driver ohci_at91_hc_driver = { |
| 228 | .description = hcd_name, |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 229 | .product_desc = "AT91 OHCI", |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 230 | .hcd_priv_size = sizeof(struct ohci_hcd), |
| 231 | |
| 232 | /* |
| 233 | * generic hardware linkage |
| 234 | */ |
| 235 | .irq = ohci_irq, |
| 236 | .flags = HCD_USB11 | HCD_MEMORY, |
| 237 | |
| 238 | /* |
| 239 | * basic lifecycle operations |
| 240 | */ |
| 241 | .start = ohci_at91_start, |
| 242 | .stop = ohci_stop, |
David Brownell | dd9048a | 2006-12-05 03:18:31 -0800 | [diff] [blame] | 243 | .shutdown = ohci_shutdown, |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 244 | |
| 245 | /* |
| 246 | * managing i/o requests and associated device resources |
| 247 | */ |
| 248 | .urb_enqueue = ohci_urb_enqueue, |
| 249 | .urb_dequeue = ohci_urb_dequeue, |
| 250 | .endpoint_disable = ohci_endpoint_disable, |
| 251 | |
| 252 | /* |
| 253 | * scheduling support |
| 254 | */ |
| 255 | .get_frame_number = ohci_get_frame, |
| 256 | |
| 257 | /* |
| 258 | * root hub support |
| 259 | */ |
| 260 | .hub_status_data = ohci_hub_status_data, |
| 261 | .hub_control = ohci_hub_control, |
David Brownell | d413984 | 2006-08-04 11:31:55 -0700 | [diff] [blame] | 262 | .hub_irq_enable = ohci_rhsc_enable, |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 263 | #ifdef CONFIG_PM |
David Brownell | 68ba61b | 2006-04-02 20:26:21 -0800 | [diff] [blame] | 264 | .bus_suspend = ohci_bus_suspend, |
| 265 | .bus_resume = ohci_bus_resume, |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 266 | #endif |
| 267 | .start_port_reset = ohci_start_port_reset, |
| 268 | }; |
| 269 | |
| 270 | /*-------------------------------------------------------------------------*/ |
| 271 | |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 272 | static int ohci_hcd_at91_drv_probe(struct platform_device *pdev) |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 273 | { |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 274 | device_init_wakeup(&pdev->dev, 1); |
| 275 | return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev); |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 276 | } |
| 277 | |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 278 | static int ohci_hcd_at91_drv_remove(struct platform_device *pdev) |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 279 | { |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 280 | device_init_wakeup(&pdev->dev, 0); |
| 281 | return usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev); |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | #ifdef CONFIG_PM |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 285 | |
David Brownell | 68ba61b | 2006-04-02 20:26:21 -0800 | [diff] [blame] | 286 | static int |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 287 | ohci_hcd_at91_drv_suspend(struct platform_device *pdev, pm_message_t mesg) |
David Brownell | 68ba61b | 2006-04-02 20:26:21 -0800 | [diff] [blame] | 288 | { |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 289 | struct usb_hcd *hcd = platform_get_drvdata(pdev); |
| 290 | struct ohci_hcd *ohci = hcd_to_ohci(hcd); |
| 291 | |
| 292 | if (device_may_wakeup(&pdev->dev)) |
| 293 | enable_irq_wake(hcd->irq); |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 294 | |
| 295 | /* |
| 296 | * The integrated transceivers seem unable to notice disconnect, |
| 297 | * reconnect, or wakeup without the 48 MHz clock active. so for |
| 298 | * correctness, always discard connection state (using reset). |
| 299 | * |
| 300 | * REVISIT: some boards will be able to turn VBUS off... |
| 301 | */ |
| 302 | if (at91_suspend_entering_slow_clock()) { |
| 303 | ohci_usb_reset (ohci); |
Andrew Victor | ed077bb | 2007-02-16 10:18:58 -0800 | [diff] [blame^] | 304 | at91_stop_clock(); |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 305 | } |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 310 | static int ohci_hcd_at91_drv_resume(struct platform_device *pdev) |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 311 | { |
Marc Pignat | 6dde896 | 2007-01-09 14:00:11 -0800 | [diff] [blame] | 312 | struct usb_hcd *hcd = platform_get_drvdata(pdev); |
| 313 | |
| 314 | if (device_may_wakeup(&pdev->dev)) |
| 315 | disable_irq_wake(hcd->irq); |
| 316 | |
Andrew Victor | ed077bb | 2007-02-16 10:18:58 -0800 | [diff] [blame^] | 317 | if (!clocked) |
| 318 | at91_start_clock(); |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | #else |
| 323 | #define ohci_hcd_at91_drv_suspend NULL |
| 324 | #define ohci_hcd_at91_drv_resume NULL |
| 325 | #endif |
| 326 | |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 327 | MODULE_ALIAS("at91_ohci"); |
David Brownell | 68ba61b | 2006-04-02 20:26:21 -0800 | [diff] [blame] | 328 | |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 329 | static struct platform_driver ohci_hcd_at91_driver = { |
| 330 | .probe = ohci_hcd_at91_drv_probe, |
| 331 | .remove = ohci_hcd_at91_drv_remove, |
Aleksey Gorelov | 64a21d0 | 2006-08-08 17:24:08 -0700 | [diff] [blame] | 332 | .shutdown = usb_hcd_platform_shutdown, |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 333 | .suspend = ohci_hcd_at91_drv_suspend, |
| 334 | .resume = ohci_hcd_at91_drv_resume, |
| 335 | .driver = { |
David Brownell | 0365ee0 | 2006-06-19 14:27:20 -0700 | [diff] [blame] | 336 | .name = "at91_ohci", |
Andrew Victor | 39a269c | 2006-01-22 10:32:13 -0800 | [diff] [blame] | 337 | .owner = THIS_MODULE, |
| 338 | }, |
| 339 | }; |
| 340 | |