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