1// SPDX-License-Identifier: GPL-1.0+
2/*
3 * OHCI HCD (Host Controller Driver) for USB.
4 *
5 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
6 * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
7 * (C) Copyright 2002 Hewlett-Packard Company
8 * (C) Copyright 2006 Sylvain Munaut <tnt@246tNt.com>
9 *
10 * Bus glue for OHCI HC on the of_platform bus
11 *
12 * Modified for of_platform bus from ohci-sa1111.c
13 *
14 * This file is licenced under the GPL.
15 */
16
17#include <linux/signal.h>
18#include <linux/of.h>
19#include <linux/of_address.h>
20#include <linux/of_irq.h>
21#include <linux/platform_device.h>
22
23static int
24ohci_ppc_of_start(struct usb_hcd *hcd)
25{
26	struct ohci_hcd	*ohci = hcd_to_ohci(hcd);
27	int		ret;
28
29	if ((ret = ohci_init(ohci)) < 0)
30		return ret;
31
32	if ((ret = ohci_run(ohci)) < 0) {
33		dev_err(hcd->self.controller, "can't start %s\n",
34			hcd->self.bus_name);
35		ohci_stop(hcd);
36		return ret;
37	}
38
39	return 0;
40}
41
42static const struct hc_driver ohci_ppc_of_hc_driver = {
43	.description =		hcd_name,
44	.product_desc =		"OF OHCI",
45	.hcd_priv_size =	sizeof(struct ohci_hcd),
46
47	/*
48	 * generic hardware linkage
49	 */
50	.irq =			ohci_irq,
51	.flags =		HCD_USB11 | HCD_DMA | HCD_MEMORY,
52
53	/*
54	 * basic lifecycle operations
55	 */
56	.start =		ohci_ppc_of_start,
57	.stop =			ohci_stop,
58	.shutdown = 		ohci_shutdown,
59
60	/*
61	 * managing i/o requests and associated device resources
62	 */
63	.urb_enqueue =		ohci_urb_enqueue,
64	.urb_dequeue =		ohci_urb_dequeue,
65	.endpoint_disable =	ohci_endpoint_disable,
66
67	/*
68	 * scheduling support
69	 */
70	.get_frame_number =	ohci_get_frame,
71
72	/*
73	 * root hub support
74	 */
75	.hub_status_data =	ohci_hub_status_data,
76	.hub_control =		ohci_hub_control,
77#ifdef	CONFIG_PM
78	.bus_suspend =		ohci_bus_suspend,
79	.bus_resume =		ohci_bus_resume,
80#endif
81	.start_port_reset =	ohci_start_port_reset,
82};
83
84
85static int ohci_hcd_ppc_of_probe(struct platform_device *op)
86{
87	struct device_node *dn = op->dev.of_node;
88	struct usb_hcd *hcd;
89	struct ohci_hcd	*ohci;
90	struct resource res;
91	int irq;
92
93	int rv;
94	int is_bigendian;
95	struct device_node *np;
96
97	if (usb_disabled())
98		return -ENODEV;
99
100	is_bigendian =
101		of_device_is_compatible(dn, "ohci-bigendian") ||
102		of_device_is_compatible(dn, "ohci-be");
103
104	dev_dbg(&op->dev, "initializing PPC-OF USB Controller\n");
105
106	rv = of_address_to_resource(dn, 0, &res);
107	if (rv)
108		return rv;
109
110	hcd = usb_create_hcd(&ohci_ppc_of_hc_driver, &op->dev, "PPC-OF USB");
111	if (!hcd)
112		return -ENOMEM;
113
114	hcd->rsrc_start = res.start;
115	hcd->rsrc_len = resource_size(&res);
116
117	hcd->regs = devm_ioremap_resource(&op->dev, &res);
118	if (IS_ERR(hcd->regs)) {
119		rv = PTR_ERR(hcd->regs);
120		goto err_rmr;
121	}
122
123	irq = irq_of_parse_and_map(dn, 0);
124	if (!irq) {
125		dev_err(&op->dev, "%s: irq_of_parse_and_map failed\n",
126			__FILE__);
127		rv = -EBUSY;
128		goto err_rmr;
129	}
130
131	ohci = hcd_to_ohci(hcd);
132	if (is_bigendian) {
133		ohci->flags |= OHCI_QUIRK_BE_MMIO | OHCI_QUIRK_BE_DESC;
134		if (of_device_is_compatible(dn, "fsl,mpc5200-ohci"))
135			ohci->flags |= OHCI_QUIRK_FRAME_NO;
136		if (of_device_is_compatible(dn, "mpc5200-ohci"))
137			ohci->flags |= OHCI_QUIRK_FRAME_NO;
138	}
139
140	ohci_hcd_init(ohci);
141
142	rv = usb_add_hcd(hcd, irq, 0);
143	if (rv == 0) {
144		device_wakeup_enable(hcd->self.controller);
145		return 0;
146	}
147
148	/* by now, 440epx is known to show usb_23 erratum */
149	np = of_find_compatible_node(NULL, NULL, "ibm,usb-ehci-440epx");
150
151	/* Work around - At this point ohci_run has executed, the
152	* controller is running, everything, the root ports, etc., is
153	* set up.  If the ehci driver is loaded, put the ohci core in
154	* the suspended state.  The ehci driver will bring it out of
155	* suspended state when / if a non-high speed USB device is
156	* attached to the USB Host port.  If the ehci driver is not
157	* loaded, do nothing. request_mem_region is used to test if
158	* the ehci driver is loaded.
159	*/
160	if (np !=  NULL) {
161		if (!of_address_to_resource(np, 0, &res)) {
162			if (!request_mem_region(res.start, 0x4, hcd_name)) {
163				writel_be((readl_be(&ohci->regs->control) |
164					OHCI_USB_SUSPEND), &ohci->regs->control);
165					(void) readl_be(&ohci->regs->control);
166			} else
167				release_mem_region(res.start, 0x4);
168		} else
169			pr_debug("%s: cannot get ehci offset from fdt\n", __FILE__);
170		of_node_put(np);
171	}
172
173	irq_dispose_mapping(irq);
174err_rmr:
175 	usb_put_hcd(hcd);
176
177	return rv;
178}
179
180static void ohci_hcd_ppc_of_remove(struct platform_device *op)
181{
182	struct usb_hcd *hcd = platform_get_drvdata(op);
183
184	dev_dbg(&op->dev, "stopping PPC-OF USB Controller\n");
185
186	usb_remove_hcd(hcd);
187
188	irq_dispose_mapping(hcd->irq);
189
190	usb_put_hcd(hcd);
191}
192
193static const struct of_device_id ohci_hcd_ppc_of_match[] = {
194#ifdef CONFIG_USB_OHCI_HCD_PPC_OF_BE
195	{
196		.name = "usb",
197		.compatible = "ohci-bigendian",
198	},
199	{
200		.name = "usb",
201		.compatible = "ohci-be",
202	},
203#endif
204#ifdef CONFIG_USB_OHCI_HCD_PPC_OF_LE
205	{
206		.name = "usb",
207		.compatible = "ohci-littledian",
208	},
209	{
210		.name = "usb",
211		.compatible = "ohci-le",
212	},
213#endif
214	{},
215};
216MODULE_DEVICE_TABLE(of, ohci_hcd_ppc_of_match);
217
218#if	!defined(CONFIG_USB_OHCI_HCD_PPC_OF_BE) && \
219	!defined(CONFIG_USB_OHCI_HCD_PPC_OF_LE)
220#error "No endianness selected for ppc-of-ohci"
221#endif
222
223
224static struct platform_driver ohci_hcd_ppc_of_driver = {
225	.probe		= ohci_hcd_ppc_of_probe,
226	.remove_new	= ohci_hcd_ppc_of_remove,
227	.shutdown	= usb_hcd_platform_shutdown,
228	.driver = {
229		.name = "ppc-of-ohci",
230		.of_match_table = ohci_hcd_ppc_of_match,
231	},
232};
233
234