1/*
2 * OHCI HCD (Host Controller Driver) for USB.
3 *
4 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5 * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
6 * (C) Copyright 2002 Hewlett-Packard Company
7 *
8 * Bus Glue for Moschip MCS814x.
9 *
10 * Written by Christopher Hoover <ch@hpl.hp.com>
11 * Based on fragments of previous driver by Russell King et al.
12 *
13 * Modified for LH7A404 from ohci-sa1111.c
14 *  by Durgesh Pattamatta <pattamattad@sharpsec.com>
15 *
16 * Modified for pxa27x from ohci-lh7a404.c
17 *  by Nick Bane <nick@cecomputing.co.uk> 26-8-2004
18 *
19 * Modified for mcs814x from ohci-mcs814x.c
20 *  by Lennert Buytenhek <buytenh@wantstofly.org> 28-2-2006
21 *  Based on an earlier driver by Ray Lehtiniemi
22 *
23 * This file is licenced under the GPL.
24 */
25
26#include <linux/device.h>
27#include <linux/signal.h>
28#include <linux/platform_device.h>
29#include <linux/of.h>
30
31static int usb_hcd_mcs814x_probe(const struct hc_driver *driver,
32			 struct platform_device *pdev)
33{
34	int retval;
35	struct usb_hcd *hcd;
36
37	if (pdev->resource[1].flags != IORESOURCE_IRQ) {
38		pr_debug("resource[1] is not IORESOURCE_IRQ");
39		return -ENOMEM;
40	}
41
42	pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
43	pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
44
45	hcd = usb_create_hcd(driver, &pdev->dev, "mcs814x");
46	if (hcd == NULL)
47		return -ENOMEM;
48
49	hcd->rsrc_start = pdev->resource[0].start;
50	hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;
51	if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
52		usb_put_hcd(hcd);
53		retval = -EBUSY;
54		goto err1;
55	}
56
57	hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
58	if (hcd->regs == NULL) {
59		pr_debug("ioremap failed");
60		retval = -ENOMEM;
61		goto err2;
62	}
63
64	ohci_hcd_init(hcd_to_ohci(hcd));
65
66	retval = usb_add_hcd(hcd, pdev->resource[1].start, IRQF_DISABLED);
67	if (retval == 0)
68		return retval;
69
70	iounmap(hcd->regs);
71err2:
72	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
73err1:
74	usb_put_hcd(hcd);
75
76	return retval;
77}
78
79static void usb_hcd_mcs814x_remove(struct usb_hcd *hcd,
80			struct platform_device *pdev)
81{
82	usb_remove_hcd(hcd);
83	iounmap(hcd->regs);
84	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
85	usb_put_hcd(hcd);
86}
87
88static int __devinit ohci_mcs814x_start(struct usb_hcd *hcd)
89{
90	struct ohci_hcd *ohci = hcd_to_ohci(hcd);
91	int ret;
92
93	ret = ohci_init(ohci);
94	if (ret < 0)
95		return ret;
96
97	ret = ohci_run(ohci);
98	if (ret < 0) {
99		ohci_err(ohci, "can't start %s", hcd->self.bus_name);
100		ohci_stop(hcd);
101		return ret;
102	}
103
104	return 0;
105}
106
107static struct hc_driver ohci_mcs814x_hc_driver = {
108	.description		= hcd_name,
109	.product_desc		= "MCS814X OHCI",
110	.hcd_priv_size		= sizeof(struct ohci_hcd),
111	.irq			= ohci_irq,
112	.flags			= HCD_USB11 | HCD_MEMORY,
113	.start			= ohci_mcs814x_start,
114	.stop			= ohci_stop,
115	.shutdown		= ohci_shutdown,
116	.urb_enqueue		= ohci_urb_enqueue,
117	.urb_dequeue		= ohci_urb_dequeue,
118	.endpoint_disable	= ohci_endpoint_disable,
119	.get_frame_number	= ohci_get_frame,
120	.hub_status_data	= ohci_hub_status_data,
121	.hub_control		= ohci_hub_control,
122#ifdef CONFIG_PM
123	.bus_suspend		= ohci_bus_suspend,
124	.bus_resume		= ohci_bus_resume,
125#endif
126	.start_port_reset	= ohci_start_port_reset,
127};
128
129extern int usb_disabled(void);
130
131static int ohci_hcd_mcs814x_drv_probe(struct platform_device *pdev)
132{
133	int ret;
134
135	ret = -ENODEV;
136	if (!usb_disabled())
137		ret = usb_hcd_mcs814x_probe(&ohci_mcs814x_hc_driver, pdev);
138
139	return ret;
140}
141
142static int ohci_hcd_mcs814x_drv_remove(struct platform_device *pdev)
143{
144	struct usb_hcd *hcd = platform_get_drvdata(pdev);
145
146	usb_hcd_mcs814x_remove(hcd, pdev);
147
148	return 0;
149}
150
151#ifdef CONFIG_PM
152static int ohci_hcd_mcs814x_drv_suspend(struct platform_device *pdev, pm_message_t state)
153{
154	struct usb_hcd *hcd = platform_get_drvdata(pdev);
155	struct ohci_hcd *ohci = hcd_to_ohci(hcd);
156
157	if (time_before(jiffies, ohci->next_statechange))
158		msleep(5);
159	ohci->next_statechange = jiffies;
160
161	hcd->state = HC_STATE_SUSPENDED;
162
163	return 0;
164}
165
166static int ohci_hcd_mcs814x_drv_resume(struct platform_device *pdev)
167{
168	struct usb_hcd *hcd = platform_get_drvdata(pdev);
169	struct ohci_hcd *ohci = hcd_to_ohci(hcd);
170	int status;
171
172	if (time_before(jiffies, ohci->next_statechange))
173		msleep(5);
174	ohci->next_statechange = jiffies;
175
176	ohci_finish_controller_resume(hcd);
177	return 0;
178}
179#endif
180
181static const struct of_device_id mcs814x_ohci_id[] = {
182	{ .compatible = "moschip,mcs814x-ohci" },
183	{ .compatible = "ohci-le" },
184	{ /* sentinel */ },
185};
186
187static struct platform_driver ohci_hcd_mcs814x_driver = {
188	.probe		= ohci_hcd_mcs814x_drv_probe,
189	.remove		= ohci_hcd_mcs814x_drv_remove,
190	.shutdown	= usb_hcd_platform_shutdown,
191#ifdef CONFIG_PM
192	.suspend	= ohci_hcd_mcs814x_drv_suspend,
193	.resume		= ohci_hcd_mcs814x_drv_resume,
194#endif
195	.driver		= {
196		.name	= "mcs814x-ohci",
197		.owner	= THIS_MODULE,
198		.of_match_table = mcs814x_ohci_id,
199	},
200};
201
202MODULE_ALIAS("platform:mcs814x-ohci");
203