xref: /linux/drivers/usb/host/ehci-ps3.c (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  PS3 EHCI Host Controller driver
4  *
5  *  Copyright (C) 2006 Sony Computer Entertainment Inc.
6  *  Copyright 2006 Sony Corp.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; version 2 of the License.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 
22 #include <asm/firmware.h>
23 #include <asm/ps3.h>
24 
25 static void ps3_ehci_setup_insnreg(struct ehci_hcd *ehci)
26 {
27 	/* PS3 HC internal setup register offsets. */
28 
29 	enum ps3_ehci_hc_insnreg {
30 		ps3_ehci_hc_insnreg01 = 0x084,
31 		ps3_ehci_hc_insnreg02 = 0x088,
32 		ps3_ehci_hc_insnreg03 = 0x08c,
33 	};
34 
35 	/* PS3 EHCI HC errata fix 316 - The PS3 EHCI HC will reset its
36 	 * internal INSNREGXX setup regs back to the chip default values
37 	 * on Host Controller Reset (CMD_RESET) or Light Host Controller
38 	 * Reset (CMD_LRESET).  The work-around for this is for the HC
39 	 * driver to re-initialise these regs when ever the HC is reset.
40 	 */
41 
42 	/* Set burst transfer counts to 256 out, 32 in. */
43 
44 	writel_be(0x01000020, (void __iomem *)ehci->regs +
45 		ps3_ehci_hc_insnreg01);
46 
47 	/* Enable burst transfer counts. */
48 
49 	writel_be(0x00000001, (void __iomem *)ehci->regs +
50 		ps3_ehci_hc_insnreg03);
51 }
52 
53 static int ps3_ehci_hc_reset(struct usb_hcd *hcd)
54 {
55 	int result;
56 	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
57 
58 	ehci->big_endian_mmio = 1;
59 	ehci->caps = hcd->regs;
60 
61 	result = ehci_setup(hcd);
62 	if (result)
63 		return result;
64 
65 	ps3_ehci_setup_insnreg(ehci);
66 
67 	return result;
68 }
69 
70 static const struct hc_driver ps3_ehci_hc_driver = {
71 	.description		= hcd_name,
72 	.product_desc		= "PS3 EHCI Host Controller",
73 	.hcd_priv_size		= sizeof(struct ehci_hcd),
74 	.irq			= ehci_irq,
75 	.flags			= HCD_MEMORY | HCD_USB2 | HCD_BH,
76 	.reset			= ps3_ehci_hc_reset,
77 	.start			= ehci_run,
78 	.stop			= ehci_stop,
79 	.shutdown		= ehci_shutdown,
80 	.urb_enqueue		= ehci_urb_enqueue,
81 	.urb_dequeue		= ehci_urb_dequeue,
82 	.endpoint_disable	= ehci_endpoint_disable,
83 	.endpoint_reset		= ehci_endpoint_reset,
84 	.get_frame_number	= ehci_get_frame,
85 	.hub_status_data	= ehci_hub_status_data,
86 	.hub_control		= ehci_hub_control,
87 #if defined(CONFIG_PM)
88 	.bus_suspend		= ehci_bus_suspend,
89 	.bus_resume		= ehci_bus_resume,
90 #endif
91 	.relinquish_port	= ehci_relinquish_port,
92 	.port_handed_over	= ehci_port_handed_over,
93 
94 	.clear_tt_buffer_complete	= ehci_clear_tt_buffer_complete,
95 };
96 
97 static int ps3_ehci_probe(struct ps3_system_bus_device *dev)
98 {
99 	int result;
100 	struct usb_hcd *hcd;
101 	unsigned int virq;
102 	static u64 dummy_mask = DMA_BIT_MASK(32);
103 
104 	if (usb_disabled()) {
105 		result = -ENODEV;
106 		goto fail_start;
107 	}
108 
109 	result = ps3_open_hv_device(dev);
110 
111 	if (result) {
112 		dev_dbg(&dev->core, "%s:%d: ps3_open_hv_device failed\n",
113 			__func__, __LINE__);
114 		goto fail_open;
115 	}
116 
117 	result = ps3_dma_region_create(dev->d_region);
118 
119 	if (result) {
120 		dev_dbg(&dev->core, "%s:%d: ps3_dma_region_create failed: "
121 			"(%d)\n", __func__, __LINE__, result);
122 		BUG_ON("check region type");
123 		goto fail_dma_region;
124 	}
125 
126 	result = ps3_mmio_region_create(dev->m_region);
127 
128 	if (result) {
129 		dev_dbg(&dev->core, "%s:%d: ps3_map_mmio_region failed\n",
130 			__func__, __LINE__);
131 		result = -EPERM;
132 		goto fail_mmio_region;
133 	}
134 
135 	dev_dbg(&dev->core, "%s:%d: mmio mapped_addr %lxh\n", __func__,
136 		__LINE__, dev->m_region->lpar_addr);
137 
138 	result = ps3_io_irq_setup(PS3_BINDING_CPU_ANY, dev->interrupt_id, &virq);
139 
140 	if (result) {
141 		dev_dbg(&dev->core, "%s:%d: ps3_construct_io_irq(%d) failed.\n",
142 			__func__, __LINE__, virq);
143 		result = -EPERM;
144 		goto fail_irq;
145 	}
146 
147 	dev->core.dma_mask = &dummy_mask; /* FIXME: for improper usb code */
148 
149 	hcd = usb_create_hcd(&ps3_ehci_hc_driver, &dev->core, dev_name(&dev->core));
150 
151 	if (!hcd) {
152 		dev_dbg(&dev->core, "%s:%d: usb_create_hcd failed\n", __func__,
153 			__LINE__);
154 		result = -ENOMEM;
155 		goto fail_create_hcd;
156 	}
157 
158 	hcd->rsrc_start = dev->m_region->lpar_addr;
159 	hcd->rsrc_len = dev->m_region->len;
160 
161 	if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name))
162 		dev_dbg(&dev->core, "%s:%d: request_mem_region failed\n",
163 			__func__, __LINE__);
164 
165 	hcd->regs = ioremap(dev->m_region->lpar_addr, dev->m_region->len);
166 
167 	if (!hcd->regs) {
168 		dev_dbg(&dev->core, "%s:%d: ioremap failed\n", __func__,
169 			__LINE__);
170 		result = -EPERM;
171 		goto fail_ioremap;
172 	}
173 
174 	dev_dbg(&dev->core, "%s:%d: hcd->rsrc_start %lxh\n", __func__, __LINE__,
175 		(unsigned long)hcd->rsrc_start);
176 	dev_dbg(&dev->core, "%s:%d: hcd->rsrc_len   %lxh\n", __func__, __LINE__,
177 		(unsigned long)hcd->rsrc_len);
178 	dev_dbg(&dev->core, "%s:%d: hcd->regs       %lxh\n", __func__, __LINE__,
179 		(unsigned long)hcd->regs);
180 	dev_dbg(&dev->core, "%s:%d: virq            %lu\n", __func__, __LINE__,
181 		(unsigned long)virq);
182 
183 	ps3_system_bus_set_drvdata(dev, hcd);
184 
185 	result = usb_add_hcd(hcd, virq, 0);
186 
187 	if (result) {
188 		dev_dbg(&dev->core, "%s:%d: usb_add_hcd failed (%d)\n",
189 			__func__, __LINE__, result);
190 		goto fail_add_hcd;
191 	}
192 
193 	device_wakeup_enable(hcd->self.controller);
194 	return result;
195 
196 fail_add_hcd:
197 	iounmap(hcd->regs);
198 fail_ioremap:
199 	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
200 	usb_put_hcd(hcd);
201 fail_create_hcd:
202 	ps3_io_irq_destroy(virq);
203 fail_irq:
204 	ps3_free_mmio_region(dev->m_region);
205 fail_mmio_region:
206 	ps3_dma_region_free(dev->d_region);
207 fail_dma_region:
208 	ps3_close_hv_device(dev);
209 fail_open:
210 fail_start:
211 	return result;
212 }
213 
214 static int ps3_ehci_remove(struct ps3_system_bus_device *dev)
215 {
216 	unsigned int tmp;
217 	struct usb_hcd *hcd = ps3_system_bus_get_drvdata(dev);
218 
219 	BUG_ON(!hcd);
220 
221 	dev_dbg(&dev->core, "%s:%d: regs %p\n", __func__, __LINE__, hcd->regs);
222 	dev_dbg(&dev->core, "%s:%d: irq %u\n", __func__, __LINE__, hcd->irq);
223 
224 	tmp = hcd->irq;
225 
226 	usb_remove_hcd(hcd);
227 
228 	ps3_system_bus_set_drvdata(dev, NULL);
229 
230 	BUG_ON(!hcd->regs);
231 	iounmap(hcd->regs);
232 
233 	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
234 	usb_put_hcd(hcd);
235 
236 	ps3_io_irq_destroy(tmp);
237 	ps3_free_mmio_region(dev->m_region);
238 
239 	ps3_dma_region_free(dev->d_region);
240 	ps3_close_hv_device(dev);
241 
242 	return 0;
243 }
244 
245 static int __init ps3_ehci_driver_register(struct ps3_system_bus_driver *drv)
246 {
247 	return firmware_has_feature(FW_FEATURE_PS3_LV1)
248 		? ps3_system_bus_driver_register(drv)
249 		: 0;
250 }
251 
252 static void ps3_ehci_driver_unregister(struct ps3_system_bus_driver *drv)
253 {
254 	if (firmware_has_feature(FW_FEATURE_PS3_LV1))
255 		ps3_system_bus_driver_unregister(drv);
256 }
257 
258 MODULE_ALIAS(PS3_MODULE_ALIAS_EHCI);
259 
260 static struct ps3_system_bus_driver ps3_ehci_driver = {
261 	.core.name = "ps3-ehci-driver",
262 	.core.owner = THIS_MODULE,
263 	.match_id = PS3_MATCH_ID_EHCI,
264 	.probe = ps3_ehci_probe,
265 	.remove = ps3_ehci_remove,
266 	.shutdown = ps3_ehci_remove,
267 };
268