1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * UHCI HCD (Host Controller Driver) for GRLIB GRUSBHC
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2011 Jan Andersson <jan@gaisler.com>
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * This file is based on UHCI PCI HCD:
8*4882a593Smuzhiyun * (C) Copyright 1999 Linus Torvalds
9*4882a593Smuzhiyun * (C) Copyright 1999-2002 Johannes Erdfelt, johannes@erdfelt.com
10*4882a593Smuzhiyun * (C) Copyright 1999 Randy Dunlap
11*4882a593Smuzhiyun * (C) Copyright 1999 Georg Acher, acher@in.tum.de
12*4882a593Smuzhiyun * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de
13*4882a593Smuzhiyun * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch
14*4882a593Smuzhiyun * (C) Copyright 1999 Roman Weissgaerber, weissg@vienna.at
15*4882a593Smuzhiyun * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface
16*4882a593Smuzhiyun * support from usb-ohci.c by Adam Richter, adam@yggdrasil.com).
17*4882a593Smuzhiyun * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c)
18*4882a593Smuzhiyun * (C) Copyright 2004-2007 Alan Stern, stern@rowland.harvard.edu
19*4882a593Smuzhiyun */
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #include <linux/device.h>
22*4882a593Smuzhiyun #include <linux/of_irq.h>
23*4882a593Smuzhiyun #include <linux/of_address.h>
24*4882a593Smuzhiyun #include <linux/of_platform.h>
25*4882a593Smuzhiyun
uhci_grlib_init(struct usb_hcd * hcd)26*4882a593Smuzhiyun static int uhci_grlib_init(struct usb_hcd *hcd)
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun struct uhci_hcd *uhci = hcd_to_uhci(hcd);
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun /*
31*4882a593Smuzhiyun * Probe to determine the endianness of the controller.
32*4882a593Smuzhiyun * We know that bit 7 of the PORTSC1 register is always set
33*4882a593Smuzhiyun * and bit 15 is always clear. If uhci_readw() yields a value
34*4882a593Smuzhiyun * with bit 7 (0x80) turned on then the current little-endian
35*4882a593Smuzhiyun * setting is correct. Otherwise we assume the value was
36*4882a593Smuzhiyun * byte-swapped; hence the register interface and presumably
37*4882a593Smuzhiyun * also the descriptors are big-endian.
38*4882a593Smuzhiyun */
39*4882a593Smuzhiyun if (!(uhci_readw(uhci, USBPORTSC1) & 0x80)) {
40*4882a593Smuzhiyun uhci->big_endian_mmio = 1;
41*4882a593Smuzhiyun uhci->big_endian_desc = 1;
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun uhci->rh_numports = uhci_count_ports(hcd);
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /* Set up pointers to to generic functions */
47*4882a593Smuzhiyun uhci->reset_hc = uhci_generic_reset_hc;
48*4882a593Smuzhiyun uhci->check_and_reset_hc = uhci_generic_check_and_reset_hc;
49*4882a593Smuzhiyun /* No special actions need to be taken for the functions below */
50*4882a593Smuzhiyun uhci->configure_hc = NULL;
51*4882a593Smuzhiyun uhci->resume_detect_interrupts_are_broken = NULL;
52*4882a593Smuzhiyun uhci->global_suspend_mode_is_broken = NULL;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /* Reset if the controller isn't already safely quiescent. */
55*4882a593Smuzhiyun check_and_reset_hc(uhci);
56*4882a593Smuzhiyun return 0;
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun static const struct hc_driver uhci_grlib_hc_driver = {
60*4882a593Smuzhiyun .description = hcd_name,
61*4882a593Smuzhiyun .product_desc = "GRLIB GRUSBHC UHCI Host Controller",
62*4882a593Smuzhiyun .hcd_priv_size = sizeof(struct uhci_hcd),
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun /* Generic hardware linkage */
65*4882a593Smuzhiyun .irq = uhci_irq,
66*4882a593Smuzhiyun .flags = HCD_MEMORY | HCD_DMA | HCD_USB11,
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /* Basic lifecycle operations */
69*4882a593Smuzhiyun .reset = uhci_grlib_init,
70*4882a593Smuzhiyun .start = uhci_start,
71*4882a593Smuzhiyun #ifdef CONFIG_PM
72*4882a593Smuzhiyun .pci_suspend = NULL,
73*4882a593Smuzhiyun .pci_resume = NULL,
74*4882a593Smuzhiyun .bus_suspend = uhci_rh_suspend,
75*4882a593Smuzhiyun .bus_resume = uhci_rh_resume,
76*4882a593Smuzhiyun #endif
77*4882a593Smuzhiyun .stop = uhci_stop,
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun .urb_enqueue = uhci_urb_enqueue,
80*4882a593Smuzhiyun .urb_dequeue = uhci_urb_dequeue,
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun .endpoint_disable = uhci_hcd_endpoint_disable,
83*4882a593Smuzhiyun .get_frame_number = uhci_hcd_get_frame_number,
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun .hub_status_data = uhci_hub_status_data,
86*4882a593Smuzhiyun .hub_control = uhci_hub_control,
87*4882a593Smuzhiyun };
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun
uhci_hcd_grlib_probe(struct platform_device * op)90*4882a593Smuzhiyun static int uhci_hcd_grlib_probe(struct platform_device *op)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun struct device_node *dn = op->dev.of_node;
93*4882a593Smuzhiyun struct usb_hcd *hcd;
94*4882a593Smuzhiyun struct uhci_hcd *uhci = NULL;
95*4882a593Smuzhiyun struct resource res;
96*4882a593Smuzhiyun int irq;
97*4882a593Smuzhiyun int rv;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun if (usb_disabled())
100*4882a593Smuzhiyun return -ENODEV;
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun dev_dbg(&op->dev, "initializing GRUSBHC UHCI USB Controller\n");
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun rv = of_address_to_resource(dn, 0, &res);
105*4882a593Smuzhiyun if (rv)
106*4882a593Smuzhiyun return rv;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /* usb_create_hcd requires dma_mask != NULL */
109*4882a593Smuzhiyun op->dev.dma_mask = &op->dev.coherent_dma_mask;
110*4882a593Smuzhiyun hcd = usb_create_hcd(&uhci_grlib_hc_driver, &op->dev,
111*4882a593Smuzhiyun "GRUSBHC UHCI USB");
112*4882a593Smuzhiyun if (!hcd)
113*4882a593Smuzhiyun return -ENOMEM;
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun hcd->rsrc_start = res.start;
116*4882a593Smuzhiyun hcd->rsrc_len = resource_size(&res);
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun irq = irq_of_parse_and_map(dn, 0);
119*4882a593Smuzhiyun if (irq == NO_IRQ) {
120*4882a593Smuzhiyun printk(KERN_ERR "%s: irq_of_parse_and_map failed\n", __FILE__);
121*4882a593Smuzhiyun rv = -EBUSY;
122*4882a593Smuzhiyun goto err_usb;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun hcd->regs = devm_ioremap_resource(&op->dev, &res);
126*4882a593Smuzhiyun if (IS_ERR(hcd->regs)) {
127*4882a593Smuzhiyun rv = PTR_ERR(hcd->regs);
128*4882a593Smuzhiyun goto err_irq;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun uhci = hcd_to_uhci(hcd);
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun uhci->regs = hcd->regs;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun rv = usb_add_hcd(hcd, irq, 0);
136*4882a593Smuzhiyun if (rv)
137*4882a593Smuzhiyun goto err_irq;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun device_wakeup_enable(hcd->self.controller);
140*4882a593Smuzhiyun return 0;
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun err_irq:
143*4882a593Smuzhiyun irq_dispose_mapping(irq);
144*4882a593Smuzhiyun err_usb:
145*4882a593Smuzhiyun usb_put_hcd(hcd);
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun return rv;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun
uhci_hcd_grlib_remove(struct platform_device * op)150*4882a593Smuzhiyun static int uhci_hcd_grlib_remove(struct platform_device *op)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun struct usb_hcd *hcd = platform_get_drvdata(op);
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun dev_dbg(&op->dev, "stopping GRLIB GRUSBHC UHCI USB Controller\n");
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun usb_remove_hcd(hcd);
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun irq_dispose_mapping(hcd->irq);
159*4882a593Smuzhiyun usb_put_hcd(hcd);
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun return 0;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun /* Make sure the controller is quiescent and that we're not using it
165*4882a593Smuzhiyun * any more. This is mainly for the benefit of programs which, like kexec,
166*4882a593Smuzhiyun * expect the hardware to be idle: not doing DMA or generating IRQs.
167*4882a593Smuzhiyun *
168*4882a593Smuzhiyun * This routine may be called in a damaged or failing kernel. Hence we
169*4882a593Smuzhiyun * do not acquire the spinlock before shutting down the controller.
170*4882a593Smuzhiyun */
uhci_hcd_grlib_shutdown(struct platform_device * op)171*4882a593Smuzhiyun static void uhci_hcd_grlib_shutdown(struct platform_device *op)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun struct usb_hcd *hcd = platform_get_drvdata(op);
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun uhci_hc_died(hcd_to_uhci(hcd));
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun static const struct of_device_id uhci_hcd_grlib_of_match[] = {
179*4882a593Smuzhiyun { .name = "GAISLER_UHCI", },
180*4882a593Smuzhiyun { .name = "01_027", },
181*4882a593Smuzhiyun {},
182*4882a593Smuzhiyun };
183*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, uhci_hcd_grlib_of_match);
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun static struct platform_driver uhci_grlib_driver = {
187*4882a593Smuzhiyun .probe = uhci_hcd_grlib_probe,
188*4882a593Smuzhiyun .remove = uhci_hcd_grlib_remove,
189*4882a593Smuzhiyun .shutdown = uhci_hcd_grlib_shutdown,
190*4882a593Smuzhiyun .driver = {
191*4882a593Smuzhiyun .name = "grlib-uhci",
192*4882a593Smuzhiyun .of_match_table = uhci_hcd_grlib_of_match,
193*4882a593Smuzhiyun },
194*4882a593Smuzhiyun };
195