ohci-at91.c 8.4 KB
Newer Older
1 2 3 4 5 6
/*
 * OHCI HCD (Host Controller Driver) for USB.
 *
 *  Copyright (C) 2004 SAN People (Pty) Ltd.
 *  Copyright (C) 2005 Thibaut VARENE <varenet@parisc-linux.org>
 *
7
 * AT91 Bus Glue
8 9 10 11 12 13 14 15 16 17
 *
 * Based on fragments of 2.4 driver by Rick Bronson.
 * Based on ohci-omap.c
 *
 * This file is licenced under the GPL.
 */

#include <linux/clk.h>
#include <linux/platform_device.h>

18
#include <mach/hardware.h>
19 20
#include <asm/gpio.h>

21 22
#include <mach/board.h>
#include <mach/cpu.h>
23

24 25
#ifndef CONFIG_ARCH_AT91
#error "CONFIG_ARCH_AT91 must be defined."
26 27
#endif

28 29
/* interface and function clocks; sometimes also an AHB clock */
static struct clk *iclk, *fclk, *hclk;
30
static int clocked;
31 32 33 34 35

extern int usb_disabled(void);

/*-------------------------------------------------------------------------*/

36 37
static void at91_start_clock(void)
{
H
Hong Xu 已提交
38
	if (cpu_is_at91sam9261() || cpu_is_at91sam9g10())
39 40 41 42 43 44 45 46 47 48
		clk_enable(hclk);
	clk_enable(iclk);
	clk_enable(fclk);
	clocked = 1;
}

static void at91_stop_clock(void)
{
	clk_disable(fclk);
	clk_disable(iclk);
H
Hong Xu 已提交
49
	if (cpu_is_at91sam9261() || cpu_is_at91sam9g10())
50 51 52 53
		clk_disable(hclk);
	clocked = 0;
}

54 55 56 57 58
static void at91_start_hc(struct platform_device *pdev)
{
	struct usb_hcd *hcd = platform_get_drvdata(pdev);
	struct ohci_regs __iomem *regs = hcd->regs;

59
	dev_dbg(&pdev->dev, "start\n");
60 61 62 63

	/*
	 * Start the USB clocks.
	 */
64
	at91_start_clock();
65 66 67 68 69 70 71 72 73 74 75 76

	/*
	 * The USB host controller must remain in reset.
	 */
	writel(0, &regs->control);
}

static void at91_stop_hc(struct platform_device *pdev)
{
	struct usb_hcd *hcd = platform_get_drvdata(pdev);
	struct ohci_regs __iomem *regs = hcd->regs;

77
	dev_dbg(&pdev->dev, "stop\n");
78 79 80 81 82 83 84 85 86

	/*
	 * Put the USB host controller into reset.
	 */
	writel(0, &regs->control);

	/*
	 * Stop the USB clocks.
	 */
87
	at91_stop_clock();
88 89 90 91 92
}


/*-------------------------------------------------------------------------*/

93
static void usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
94 95 96 97 98 99

/* configure so an HC device and id are always provided */
/* always called with process context; sleeping is OK */


/**
100
 * usb_hcd_at91_probe - initialize AT91-based HCDs
101 102 103 104 105 106
 * Context: !in_interrupt()
 *
 * Allocates basic resources for this USB host controller, and
 * then invokes the start() method for the HCD associated with it
 * through the hotplug entry's driver_data.
 */
107 108
static int usb_hcd_at91_probe(const struct hc_driver *driver,
			struct platform_device *pdev)
109 110 111 112 113 114 115 116 117
{
	int retval;
	struct usb_hcd *hcd = NULL;

	if (pdev->num_resources != 2) {
		pr_debug("hcd probe: invalid num_resources");
		return -ENODEV;
	}

118 119
	if ((pdev->resource[0].flags != IORESOURCE_MEM)
			|| (pdev->resource[1].flags != IORESOURCE_IRQ)) {
120 121 122 123
		pr_debug("hcd probe: invalid resource type\n");
		return -ENODEV;
	}

124
	hcd = usb_create_hcd(driver, &pdev->dev, "at91");
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	if (!hcd)
		return -ENOMEM;
	hcd->rsrc_start = pdev->resource[0].start;
	hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;

	if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
		pr_debug("request_mem_region failed\n");
		retval = -EBUSY;
		goto err1;
	}

	hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
	if (!hcd->regs) {
		pr_debug("ioremap failed\n");
		retval = -EIO;
		goto err2;
	}

	iclk = clk_get(&pdev->dev, "ohci_clk");
	fclk = clk_get(&pdev->dev, "uhpck");
H
Hong Xu 已提交
145
	if (cpu_is_at91sam9261() || cpu_is_at91sam9g10())
146
		hclk = clk_get(&pdev->dev, "hck0");
147 148 149 150

	at91_start_hc(pdev);
	ohci_hcd_init(hcd_to_ohci(hcd));

151
	retval = usb_add_hcd(hcd, pdev->resource[1].start, IRQF_SHARED);
152 153 154 155 156 157
	if (retval == 0)
		return retval;

	/* Error handling */
	at91_stop_hc(pdev);

H
Hong Xu 已提交
158
	if (cpu_is_at91sam9261() || cpu_is_at91sam9g10())
159
		clk_put(hclk);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	clk_put(fclk);
	clk_put(iclk);

	iounmap(hcd->regs);

 err2:
	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);

 err1:
	usb_put_hcd(hcd);
	return retval;
}


/* may be called with controller, bus, and devices active */

/**
177
 * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
178 179 180 181 182
 * @dev: USB Host Controller being removed
 * Context: !in_interrupt()
 *
 * Reverses the effect of usb_hcd_at91_probe(), first invoking
 * the HCD's stop() method.  It is always called from a thread
183
 * context, "rmmod" or something similar.
184 185
 *
 */
186
static void usb_hcd_at91_remove(struct usb_hcd *hcd,
187
				struct platform_device *pdev)
188 189 190 191
{
	usb_remove_hcd(hcd);
	at91_stop_hc(pdev);
	iounmap(hcd->regs);
192
	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
193
	usb_put_hcd(hcd);
194

H
Hong Xu 已提交
195
	if (cpu_is_at91sam9261() || cpu_is_at91sam9g10())
196
		clk_put(hclk);
197 198
	clk_put(fclk);
	clk_put(iclk);
199
	fclk = iclk = hclk = NULL;
200 201 202 203 204 205 206 207 208

	dev_set_drvdata(&pdev->dev, NULL);
}

/*-------------------------------------------------------------------------*/

static int __devinit
ohci_at91_start (struct usb_hcd *hcd)
{
209
	struct at91_usbh_data	*board = hcd->self.controller->platform_data;
210 211 212 213 214 215
	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
	int			ret;

	if ((ret = ohci_init(ohci)) < 0)
		return ret;

D
David Brownell 已提交
216
	ohci->num_ports = board->ports;
217

218 219 220 221 222 223 224 225 226 227 228 229
	if ((ret = ohci_run(ohci)) < 0) {
		err("can't start %s", hcd->self.bus_name);
		ohci_stop(hcd);
		return ret;
	}
	return 0;
}

/*-------------------------------------------------------------------------*/

static const struct hc_driver ohci_at91_hc_driver = {
	.description =		hcd_name,
230
	.product_desc =		"AT91 OHCI",
231 232 233 234 235 236 237 238 239 240 241 242 243
	.hcd_priv_size =	sizeof(struct ohci_hcd),

	/*
	 * generic hardware linkage
	 */
	.irq =			ohci_irq,
	.flags =		HCD_USB11 | HCD_MEMORY,

	/*
	 * basic lifecycle operations
	 */
	.start =		ohci_at91_start,
	.stop =			ohci_stop,
244
	.shutdown =		ohci_shutdown,
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263

	/*
	 * managing i/o requests and associated device resources
	 */
	.urb_enqueue =		ohci_urb_enqueue,
	.urb_dequeue =		ohci_urb_dequeue,
	.endpoint_disable =	ohci_endpoint_disable,

	/*
	 * scheduling support
	 */
	.get_frame_number =	ohci_get_frame,

	/*
	 * root hub support
	 */
	.hub_status_data =	ohci_hub_status_data,
	.hub_control =		ohci_hub_control,
#ifdef CONFIG_PM
264 265
	.bus_suspend =		ohci_bus_suspend,
	.bus_resume =		ohci_bus_resume,
266 267 268 269 270 271
#endif
	.start_port_reset =	ohci_start_port_reset,
};

/*-------------------------------------------------------------------------*/

272
static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
273
{
274 275 276 277 278 279 280 281 282
	struct at91_usbh_data	*pdata = pdev->dev.platform_data;
	int			i;

	if (pdata) {
		/* REVISIT make the driver support per-port power switching,
		 * and also overcurrent detection.  Here we assume the ports
		 * are always powered while this driver is active, and use
		 * active-low power switches.
		 */
283
		for (i = 0; i < ARRAY_SIZE(pdata->vbus_pin); i++) {
284 285 286 287 288 289 290
			if (pdata->vbus_pin[i] <= 0)
				continue;
			gpio_request(pdata->vbus_pin[i], "ohci_vbus");
			gpio_direction_output(pdata->vbus_pin[i], 0);
		}
	}

291 292
	device_init_wakeup(&pdev->dev, 1);
	return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
293 294
}

295
static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
296
{
297 298 299 300
	struct at91_usbh_data	*pdata = pdev->dev.platform_data;
	int			i;

	if (pdata) {
301
		for (i = 0; i < ARRAY_SIZE(pdata->vbus_pin); i++) {
302 303 304 305 306 307 308
			if (pdata->vbus_pin[i] <= 0)
				continue;
			gpio_direction_output(pdata->vbus_pin[i], 1);
			gpio_free(pdata->vbus_pin[i]);
		}
	}

309
	device_init_wakeup(&pdev->dev, 0);
310 311
	usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
	return 0;
312 313 314 315
}

#ifdef CONFIG_PM

316
static int
317
ohci_hcd_at91_drv_suspend(struct platform_device *pdev, pm_message_t mesg)
318
{
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
	struct usb_hcd	*hcd = platform_get_drvdata(pdev);
	struct ohci_hcd	*ohci = hcd_to_ohci(hcd);

	if (device_may_wakeup(&pdev->dev))
		enable_irq_wake(hcd->irq);

	/*
	 * The integrated transceivers seem unable to notice disconnect,
	 * reconnect, or wakeup without the 48 MHz clock active.  so for
	 * correctness, always discard connection state (using reset).
	 *
	 * REVISIT: some boards will be able to turn VBUS off...
	 */
	if (at91_suspend_entering_slow_clock()) {
		ohci_usb_reset (ohci);
334
		at91_stop_clock();
335
	}
336 337 338 339

	return 0;
}

340
static int ohci_hcd_at91_drv_resume(struct platform_device *pdev)
341
{
342 343 344 345 346
	struct usb_hcd	*hcd = platform_get_drvdata(pdev);

	if (device_may_wakeup(&pdev->dev))
		disable_irq_wake(hcd->irq);

347 348
	if (!clocked)
		at91_start_clock();
349

350
	ohci_finish_controller_resume(hcd);
351 352 353 354 355 356 357
	return 0;
}
#else
#define ohci_hcd_at91_drv_suspend NULL
#define ohci_hcd_at91_drv_resume  NULL
#endif

358
MODULE_ALIAS("platform:at91_ohci");
359

360 361 362
static struct platform_driver ohci_hcd_at91_driver = {
	.probe		= ohci_hcd_at91_drv_probe,
	.remove		= ohci_hcd_at91_drv_remove,
363
	.shutdown	= usb_hcd_platform_shutdown,
364 365 366
	.suspend	= ohci_hcd_at91_drv_suspend,
	.resume		= ohci_hcd_at91_drv_resume,
	.driver		= {
367
		.name	= "at91_ohci",
368 369 370
		.owner	= THIS_MODULE,
	},
};