1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * EHCI HCD (Host Controller Driver) for USB.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Bus Glue for Xilinx EHCI core on the of_platform bus
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Copyright (c) 2009 Xilinx, Inc.
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * Based on "ehci-ppc-of.c" by Valentine Barshak <vbarshak@ru.mvista.com>
10*4882a593Smuzhiyun * and "ehci-ppc-soc.c" by Stefan Roese <sr@denx.de>
11*4882a593Smuzhiyun * and "ohci-ppc-of.c" by Sylvain Munaut <tnt@246tNt.com>
12*4882a593Smuzhiyun */
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <linux/err.h>
15*4882a593Smuzhiyun #include <linux/signal.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include <linux/of.h>
18*4882a593Smuzhiyun #include <linux/of_platform.h>
19*4882a593Smuzhiyun #include <linux/of_address.h>
20*4882a593Smuzhiyun #include <linux/of_irq.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun /**
23*4882a593Smuzhiyun * ehci_xilinx_port_handed_over - hand the port out if failed to enable it
24*4882a593Smuzhiyun * @hcd: Pointer to the usb_hcd device to which the host controller bound
25*4882a593Smuzhiyun * @portnum:Port number to which the device is attached.
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * This function is used as a place to tell the user that the Xilinx USB host
28*4882a593Smuzhiyun * controller does support LS devices. And in an HS only configuration, it
29*4882a593Smuzhiyun * does not support FS devices either. It is hoped that this can help a
30*4882a593Smuzhiyun * confused user.
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * There are cases when the host controller fails to enable the port due to,
33*4882a593Smuzhiyun * for example, insufficient power that can be supplied to the device from
34*4882a593Smuzhiyun * the USB bus. In those cases, the messages printed here are not helpful.
35*4882a593Smuzhiyun */
ehci_xilinx_port_handed_over(struct usb_hcd * hcd,int portnum)36*4882a593Smuzhiyun static int ehci_xilinx_port_handed_over(struct usb_hcd *hcd, int portnum)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun dev_warn(hcd->self.controller, "port %d cannot be enabled\n", portnum);
39*4882a593Smuzhiyun if (hcd->has_tt) {
40*4882a593Smuzhiyun dev_warn(hcd->self.controller,
41*4882a593Smuzhiyun "Maybe you have connected a low speed device?\n");
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun dev_warn(hcd->self.controller,
44*4882a593Smuzhiyun "We do not support low speed devices\n");
45*4882a593Smuzhiyun } else {
46*4882a593Smuzhiyun dev_warn(hcd->self.controller,
47*4882a593Smuzhiyun "Maybe your device is not a high speed device?\n");
48*4882a593Smuzhiyun dev_warn(hcd->self.controller,
49*4882a593Smuzhiyun "The USB host controller does not support full speed "
50*4882a593Smuzhiyun "nor low speed devices\n");
51*4882a593Smuzhiyun dev_warn(hcd->self.controller,
52*4882a593Smuzhiyun "You can reconfigure the host controller to have "
53*4882a593Smuzhiyun "full speed support\n");
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun return 0;
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun static const struct hc_driver ehci_xilinx_of_hc_driver = {
61*4882a593Smuzhiyun .description = hcd_name,
62*4882a593Smuzhiyun .product_desc = "OF EHCI",
63*4882a593Smuzhiyun .hcd_priv_size = sizeof(struct ehci_hcd),
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun /*
66*4882a593Smuzhiyun * generic hardware linkage
67*4882a593Smuzhiyun */
68*4882a593Smuzhiyun .irq = ehci_irq,
69*4882a593Smuzhiyun .flags = HCD_MEMORY | HCD_DMA | HCD_USB2 | HCD_BH,
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun * basic lifecycle operations
73*4882a593Smuzhiyun */
74*4882a593Smuzhiyun .reset = ehci_setup,
75*4882a593Smuzhiyun .start = ehci_run,
76*4882a593Smuzhiyun .stop = ehci_stop,
77*4882a593Smuzhiyun .shutdown = ehci_shutdown,
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /*
80*4882a593Smuzhiyun * managing i/o requests and associated device resources
81*4882a593Smuzhiyun */
82*4882a593Smuzhiyun .urb_enqueue = ehci_urb_enqueue,
83*4882a593Smuzhiyun .urb_dequeue = ehci_urb_dequeue,
84*4882a593Smuzhiyun .endpoint_disable = ehci_endpoint_disable,
85*4882a593Smuzhiyun .endpoint_reset = ehci_endpoint_reset,
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /*
88*4882a593Smuzhiyun * scheduling support
89*4882a593Smuzhiyun */
90*4882a593Smuzhiyun .get_frame_number = ehci_get_frame,
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun /*
93*4882a593Smuzhiyun * root hub support
94*4882a593Smuzhiyun */
95*4882a593Smuzhiyun .hub_status_data = ehci_hub_status_data,
96*4882a593Smuzhiyun .hub_control = ehci_hub_control,
97*4882a593Smuzhiyun #ifdef CONFIG_PM
98*4882a593Smuzhiyun .bus_suspend = ehci_bus_suspend,
99*4882a593Smuzhiyun .bus_resume = ehci_bus_resume,
100*4882a593Smuzhiyun #endif
101*4882a593Smuzhiyun .relinquish_port = NULL,
102*4882a593Smuzhiyun .port_handed_over = ehci_xilinx_port_handed_over,
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
105*4882a593Smuzhiyun };
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun /**
108*4882a593Smuzhiyun * ehci_hcd_xilinx_of_probe - Probe method for the USB host controller
109*4882a593Smuzhiyun * @op: pointer to the platform_device bound to the host controller
110*4882a593Smuzhiyun *
111*4882a593Smuzhiyun * This function requests resources and sets up appropriate properties for the
112*4882a593Smuzhiyun * host controller. Because the Xilinx USB host controller can be configured
113*4882a593Smuzhiyun * as HS only or HS/FS only, it checks the configuration in the device tree
114*4882a593Smuzhiyun * entry, and sets an appropriate value for hcd->has_tt.
115*4882a593Smuzhiyun */
ehci_hcd_xilinx_of_probe(struct platform_device * op)116*4882a593Smuzhiyun static int ehci_hcd_xilinx_of_probe(struct platform_device *op)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun struct device_node *dn = op->dev.of_node;
119*4882a593Smuzhiyun struct usb_hcd *hcd;
120*4882a593Smuzhiyun struct ehci_hcd *ehci;
121*4882a593Smuzhiyun struct resource res;
122*4882a593Smuzhiyun int irq;
123*4882a593Smuzhiyun int rv;
124*4882a593Smuzhiyun int *value;
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun if (usb_disabled())
127*4882a593Smuzhiyun return -ENODEV;
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun dev_dbg(&op->dev, "initializing XILINX-OF USB Controller\n");
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun rv = of_address_to_resource(dn, 0, &res);
132*4882a593Smuzhiyun if (rv)
133*4882a593Smuzhiyun return rv;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun hcd = usb_create_hcd(&ehci_xilinx_of_hc_driver, &op->dev,
136*4882a593Smuzhiyun "XILINX-OF USB");
137*4882a593Smuzhiyun if (!hcd)
138*4882a593Smuzhiyun return -ENOMEM;
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun hcd->rsrc_start = res.start;
141*4882a593Smuzhiyun hcd->rsrc_len = resource_size(&res);
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun irq = irq_of_parse_and_map(dn, 0);
144*4882a593Smuzhiyun if (!irq) {
145*4882a593Smuzhiyun dev_err(&op->dev, "%s: irq_of_parse_and_map failed\n",
146*4882a593Smuzhiyun __FILE__);
147*4882a593Smuzhiyun rv = -EBUSY;
148*4882a593Smuzhiyun goto err_irq;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun hcd->regs = devm_ioremap_resource(&op->dev, &res);
152*4882a593Smuzhiyun if (IS_ERR(hcd->regs)) {
153*4882a593Smuzhiyun rv = PTR_ERR(hcd->regs);
154*4882a593Smuzhiyun goto err_irq;
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun ehci = hcd_to_ehci(hcd);
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun /* This core always has big-endian register interface and uses
160*4882a593Smuzhiyun * big-endian memory descriptors.
161*4882a593Smuzhiyun */
162*4882a593Smuzhiyun ehci->big_endian_mmio = 1;
163*4882a593Smuzhiyun ehci->big_endian_desc = 1;
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun /* Check whether the FS support option is selected in the hardware.
166*4882a593Smuzhiyun */
167*4882a593Smuzhiyun value = (int *)of_get_property(dn, "xlnx,support-usb-fs", NULL);
168*4882a593Smuzhiyun if (value && (*value == 1)) {
169*4882a593Smuzhiyun ehci_dbg(ehci, "USB host controller supports FS devices\n");
170*4882a593Smuzhiyun hcd->has_tt = 1;
171*4882a593Smuzhiyun } else {
172*4882a593Smuzhiyun ehci_dbg(ehci,
173*4882a593Smuzhiyun "USB host controller is HS only\n");
174*4882a593Smuzhiyun hcd->has_tt = 0;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun /* Debug registers are at the first 0x100 region
178*4882a593Smuzhiyun */
179*4882a593Smuzhiyun ehci->caps = hcd->regs + 0x100;
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun rv = usb_add_hcd(hcd, irq, 0);
182*4882a593Smuzhiyun if (rv == 0) {
183*4882a593Smuzhiyun device_wakeup_enable(hcd->self.controller);
184*4882a593Smuzhiyun return 0;
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun err_irq:
188*4882a593Smuzhiyun usb_put_hcd(hcd);
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun return rv;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun /**
194*4882a593Smuzhiyun * ehci_hcd_xilinx_of_remove - shutdown hcd and release resources
195*4882a593Smuzhiyun * @op: pointer to platform_device structure that is to be removed
196*4882a593Smuzhiyun *
197*4882a593Smuzhiyun * Remove the hcd structure, and release resources that has been requested
198*4882a593Smuzhiyun * during probe.
199*4882a593Smuzhiyun */
ehci_hcd_xilinx_of_remove(struct platform_device * op)200*4882a593Smuzhiyun static int ehci_hcd_xilinx_of_remove(struct platform_device *op)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun struct usb_hcd *hcd = platform_get_drvdata(op);
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun dev_dbg(&op->dev, "stopping XILINX-OF USB Controller\n");
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun usb_remove_hcd(hcd);
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun usb_put_hcd(hcd);
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun return 0;
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun static const struct of_device_id ehci_hcd_xilinx_of_match[] = {
214*4882a593Smuzhiyun {.compatible = "xlnx,xps-usb-host-1.00.a",},
215*4882a593Smuzhiyun {},
216*4882a593Smuzhiyun };
217*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, ehci_hcd_xilinx_of_match);
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun static struct platform_driver ehci_hcd_xilinx_of_driver = {
220*4882a593Smuzhiyun .probe = ehci_hcd_xilinx_of_probe,
221*4882a593Smuzhiyun .remove = ehci_hcd_xilinx_of_remove,
222*4882a593Smuzhiyun .shutdown = usb_hcd_platform_shutdown,
223*4882a593Smuzhiyun .driver = {
224*4882a593Smuzhiyun .name = "xilinx-of-ehci",
225*4882a593Smuzhiyun .of_match_table = ehci_hcd_xilinx_of_match,
226*4882a593Smuzhiyun },
227*4882a593Smuzhiyun };
228