1/*
2 * MCS814X EHCI Host Controller Driver
3 *
4 * Based on "ehci-fsl.c" by Randy Vinson <rvinson@mvista.com>
5 *
6 * 2007 (c) MontaVista Software, Inc. This file is licensed under
7 * the terms of the GNU General Public License version 2. This program
8 * is licensed "as is" without any warranty of any kind, whether express
9 * or implied.
10 */
11
12#include <linux/platform_device.h>
13#include <linux/of.h>
14
15#define MCS814X_EHCI_CAPS_OFFSET	0x68
16
17static int mcs814x_ehci_init(struct usb_hcd *hcd)
18{
19	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
20	int retval = 0;
21
22	ehci->caps = hcd->regs + MCS814X_EHCI_CAPS_OFFSET;
23	ehci->regs = hcd->regs
24		+ HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
25	ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
26	ehci_reset(ehci);
27
28	retval = ehci_init(hcd);
29	if (retval) {
30		pr_err("ehci_init failed\n");
31		return retval;
32	}
33
34	ehci_port_power(ehci, 0);
35
36	return retval;
37}
38
39static const struct hc_driver mcs814x_ehci_hc_driver = {
40	.description		= hcd_name,
41	.product_desc		= "MCS814X EHCI Host Controller",
42	.hcd_priv_size		= sizeof(struct ehci_hcd),
43	.irq			= ehci_irq,
44	.flags			= HCD_MEMORY | HCD_USB2,
45	.reset			= mcs814x_ehci_init,
46	.start			= ehci_run,
47	.stop			= ehci_stop,
48	.shutdown		= ehci_shutdown,
49	.urb_enqueue		= ehci_urb_enqueue,
50	.urb_dequeue		= ehci_urb_dequeue,
51	.endpoint_disable	= ehci_endpoint_disable,
52	.get_frame_number	= ehci_get_frame,
53	.hub_status_data	= ehci_hub_status_data,
54	.hub_control		= ehci_hub_control,
55#if defined(CONFIG_PM)
56	.bus_suspend		= ehci_bus_suspend,
57	.bus_resume		= ehci_bus_resume,
58#endif
59	.relinquish_port	= ehci_relinquish_port,
60	.port_handed_over	= ehci_port_handed_over,
61
62	.clear_tt_buffer_complete	= ehci_clear_tt_buffer_complete,
63};
64
65static int mcs814x_ehci_probe(struct platform_device *pdev)
66{
67	struct usb_hcd *hcd;
68	const struct hc_driver *driver = &mcs814x_ehci_hc_driver;
69	struct resource *res;
70	int irq;
71	int retval;
72
73	if (usb_disabled())
74		return -ENODEV;
75
76	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
77	if (!res) {
78		dev_err(&pdev->dev,
79			"Found HC with no IRQ. Check %s setup!\n",
80			dev_name(&pdev->dev));
81		return -ENODEV;
82	}
83	irq = res->start;
84
85	pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
86	pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
87
88	hcd = usb_create_hcd(driver, &pdev->dev, dev_name(&pdev->dev));
89	if (!hcd) {
90		retval = -ENOMEM;
91		goto fail_create_hcd;
92	}
93
94	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
95	if (!res) {
96		dev_err(&pdev->dev,
97			"Found HC with no register addr. Check %s setup!\n",
98			dev_name(&pdev->dev));
99		retval = -ENODEV;
100		goto fail_request_resource;
101	}
102	hcd->rsrc_start = res->start;
103	hcd->rsrc_len = resource_size(res);
104
105	if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
106				driver->description)) {
107		dev_dbg(&pdev->dev, "controller already in use\n");
108		retval = -EBUSY;
109		goto fail_request_resource;
110	}
111
112	hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len);
113	if (hcd->regs == NULL) {
114		dev_dbg(&pdev->dev, "error mapping memory\n");
115		retval = -EFAULT;
116		goto fail_ioremap;
117	}
118
119	retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
120	if (retval)
121		goto fail_add_hcd;
122
123	dev_info(&pdev->dev, "added MCS814X EHCI driver\n");
124
125	return retval;
126
127fail_add_hcd:
128	iounmap(hcd->regs);
129fail_ioremap:
130	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
131fail_request_resource:
132	usb_put_hcd(hcd);
133fail_create_hcd:
134	dev_err(&pdev->dev, "init %s fail, %d\n", dev_name(&pdev->dev), retval);
135	return retval;
136}
137
138static int mcs814x_ehci_remove(struct platform_device *pdev)
139{
140	struct usb_hcd *hcd = platform_get_drvdata(pdev);
141
142	usb_remove_hcd(hcd);
143	iounmap(hcd->regs);
144	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
145	usb_put_hcd(hcd);
146
147	return 0;
148}
149
150MODULE_ALIAS("platform:mcs814x-ehci");
151
152static const struct of_device_id mcs814x_ehci_id[] = {
153	{ .compatible = "moschip,mcs814x-ehci" },
154	{ .compatible = "usb-ehci" },
155	{ /* sentinel */ },
156};
157
158static struct platform_driver mcs814x_ehci_driver = {
159	.probe = mcs814x_ehci_probe,
160	.remove = mcs814x_ehci_remove,
161	.driver = {
162		.name = "mcs814x-ehci",
163		.of_match_table = mcs814x_ehci_id,
164	},
165};
166