xref: /freebsd/sys/arm/xilinx/zy7_ehci.c (revision 2ff63af9b88c7413b7d71715b5532625752a248e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012-2013 Thomas Skibo
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * A host-controller driver for Zynq-7000's USB OTG controller.
31  *
32  * Reference: Zynq-7000 All Programmable SoC Technical Reference Manual.
33  * (v1.4) November 16, 2012.  Xilinx doc UG585.  Ch. 15 covers the USB
34  * controller and register definitions are in appendix B.34.
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/bus.h>
43 #include <sys/conf.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/module.h>
47 #include <sys/mutex.h>
48 #include <sys/condvar.h>
49 #include <sys/resource.h>
50 #include <sys/rman.h>
51 
52 #include <machine/bus.h>
53 #include <machine/resource.h>
54 #include <machine/stdarg.h>
55 
56 #include <dev/ofw/ofw_bus.h>
57 #include <dev/ofw/ofw_bus_subr.h>
58 
59 #include <dev/usb/usb.h>
60 #include <dev/usb/usbdi.h>
61 
62 #include <dev/usb/usb_core.h>
63 #include <dev/usb/usb_busdma.h>
64 #include <dev/usb/usb_process.h>
65 #include <dev/usb/usb_util.h>
66 
67 #include <dev/usb/usb_controller.h>
68 #include <dev/usb/usb_bus.h>
69 #include <dev/usb/controller/ehci.h>
70 #include <dev/usb/controller/ehcireg.h>
71 
72 /* Register definitions. */
73 #define ZY7_USB_ID				0x0000
74 #define ZY7_USB_HWGENERAL			0x0004
75 #define ZY7_USB_HWHOST				0x0008
76 #define ZY7_USB_HWDEVICE			0x000c
77 #define ZY7_USB_HWTXBUF				0x0010
78 #define ZY7_USB_HWRXBUF				0x0014
79 #define ZY7_USB_GPTIMER0LD			0x0080
80 #define ZY7_USB_GPTIMER0CTRL			0x0084
81 #define ZY7_USB_GPTIMER1LD			0x0088
82 #define ZY7_USB_GPTIMER1CTRL			0x008c
83 #define ZY7_USB_SBUSCFG				0x0090
84 #define ZY7_USB_CAPLENGTH_HCIVERSION		0x0100
85 #define ZY7_USB_HCSPARAMS			0x0104
86 #define ZY7_USB_HCCPARAMS			0x0108
87 #define ZY7_USB_DCIVERSION			0x0120
88 #define ZY7_USB_DCCPARAMS			0x0124
89 #define ZY7_USB_USBCMD				0x0140
90 #define ZY7_USB_USBSTS				0x0144
91 #define ZY7_USB_USBINTR				0x0148
92 #define ZY7_USB_FRINDEX				0x014c
93 #define ZY7_USB_PERIODICLISTBASE_DEICEADDR 	0x0154
94 #define ZY7_USB_ASYNCLISTADDR_ENDPOINTLISTADDR 	0x0158
95 #define ZY7_USB_TTCTRL				0x015c
96 #define ZY7_USB_BURSTSIZE			0x0160
97 #define ZY7_USB_TXFILLTUNING			0x0164
98 #define   ZY7_USB_TXFILLTUNING_TXFIFOTHRES_SHFT		16
99 #define   ZY7_USB_TXFILLTUNING_TXFIFOTHRES_MASK		(0x3f<<16)
100 #define ZY7_USB_TXTFILLTUNING			0x0168
101 #define ZY7_USB_IC_USB				0x016c
102 #define ZY7_USB_ULPI_VIEWPORT			0x0170
103 #define   ZY7_USB_ULPI_VIEWPORT_WU			(1<<31)
104 #define   ZY7_USB_ULPI_VIEWPORT_RUN			(1<<30)
105 #define   ZY7_USB_ULPI_VIEWPORT_RW			(1<<29)
106 #define   ZY7_USB_ULPI_VIEWPORT_SS			(1<<27)
107 #define   ZY7_USB_ULPI_VIEWPORT_PORT_MASK		(7<<24)
108 #define   ZY7_USB_ULPI_VIEWPORT_PORT_SHIFT		24
109 #define   ZY7_USB_ULPI_VIEWPORT_ADDR_MASK		(0xff<<16)
110 #define   ZY7_USB_ULPI_VIEWPORT_ADDR_SHIFT		16
111 #define   ZY7_USB_ULPI_VIEWPORT_DATARD_MASK		(0xff<<8)
112 #define   ZY7_USB_ULPI_VIEWPORT_DATARD_SHIFT		8
113 #define   ZY7_USB_ULPI_VIEWPORT_DATAWR_MASK		(0xff<<0)
114 #define   ZY7_USB_ULPI_VIEWPORT_DATAWR_SHIFT		0
115 #define ZY7_USB_ENDPTNAK			0x0178
116 #define ZY7_USB_ENDPTNAKEN			0x017c
117 #define ZY7_USB_CONFIGFLAG			0x0180
118 #define ZY7_USB_PORTSC(n)			(0x0180+4*(n))
119 #define   ZY7_USB_PORTSC_PTS_MASK			(3<<30)
120 #define   ZY7_USB_PORTSC_PTS_SHIFT			30
121 #define   ZY7_USB_PORTSC_PTS_UTMI			(0<<30)
122 #define   ZY7_USB_PORTSC_PTS_ULPI			(2<<30)
123 #define   ZY7_USB_PORTSC_PTS_SERIAL			(3<<30)
124 #define   ZY7_USB_PORTSC_PTW				(1<<28)
125 #define   ZY7_USB_PORTSC_PTS2				(1<<25)
126 #define ZY7_USB_OTGSC				0x01a4
127 #define ZY7_USB_USBMODE				0x01a8
128 #define ZY7_USB_ENDPTSETUPSTAT			0x01ac
129 #define ZY7_USB_ENDPTPRIME			0x01b0
130 #define ZY7_USB_ENDPTFLUSH			0x01b4
131 #define ZY7_USB_ENDPTSTAT			0x01b8
132 #define ZY7_USB_ENDPTCOMPLETE			0x01bc
133 #define ZY7_USB_ENDPTCTRL(n)			(0x01c0+4*(n))
134 
135 #define EHCI_REG_OFFSET	ZY7_USB_CAPLENGTH_HCIVERSION
136 #define EHCI_REG_SIZE	0x100
137 
138 static void
139 zy7_ehci_post_reset(struct ehci_softc *ehci_softc)
140 {
141 	uint32_t usbmode;
142 
143 	/* Force HOST mode */
144 	usbmode = EOREAD4(ehci_softc, EHCI_USBMODE_NOLPM);
145 	usbmode &= ~EHCI_UM_CM;
146 	usbmode |= EHCI_UM_CM_HOST;
147 	EOWRITE4(ehci_softc, EHCI_USBMODE_NOLPM, usbmode);
148 }
149 
150 static int
151 zy7_phy_config(device_t dev, bus_space_tag_t io_tag, bus_space_handle_t bsh)
152 {
153 	phandle_t node;
154 	char buf[64];
155 	uint32_t portsc;
156 	int tries;
157 
158 	node = ofw_bus_get_node(dev);
159 
160 	if (OF_getprop(node, "phy_type", buf, sizeof(buf)) > 0) {
161 		portsc = bus_space_read_4(io_tag, bsh, ZY7_USB_PORTSC(1));
162 		portsc &= ~(ZY7_USB_PORTSC_PTS_MASK | ZY7_USB_PORTSC_PTW |
163 			    ZY7_USB_PORTSC_PTS2);
164 
165 		if (strcmp(buf,"ulpi") == 0)
166 			portsc |= ZY7_USB_PORTSC_PTS_ULPI;
167 		else if (strcmp(buf,"utmi") == 0)
168 			portsc |= ZY7_USB_PORTSC_PTS_UTMI;
169 		else if (strcmp(buf,"utmi-wide") == 0)
170 			portsc |= (ZY7_USB_PORTSC_PTS_UTMI |
171 				   ZY7_USB_PORTSC_PTW);
172 		else if (strcmp(buf, "serial") == 0)
173 			portsc |= ZY7_USB_PORTSC_PTS_SERIAL;
174 
175 		bus_space_write_4(io_tag, bsh, ZY7_USB_PORTSC(1), portsc);
176 	}
177 
178 	if (OF_getprop(node, "phy_vbus_ext", buf, sizeof(buf)) >= 0) {
179 		/* Tell PHY that VBUS is supplied externally. */
180 		bus_space_write_4(io_tag, bsh, ZY7_USB_ULPI_VIEWPORT,
181 				  ZY7_USB_ULPI_VIEWPORT_RUN |
182 				  ZY7_USB_ULPI_VIEWPORT_RW |
183 				  (0 << ZY7_USB_ULPI_VIEWPORT_PORT_SHIFT) |
184 				  (0x0b << ZY7_USB_ULPI_VIEWPORT_ADDR_SHIFT) |
185 				  (0x60 << ZY7_USB_ULPI_VIEWPORT_DATAWR_SHIFT)
186 			);
187 
188 		tries = 100;
189 		while ((bus_space_read_4(io_tag, bsh, ZY7_USB_ULPI_VIEWPORT) &
190 			ZY7_USB_ULPI_VIEWPORT_RUN) != 0) {
191 			if (--tries < 0)
192 				return (-1);
193 			DELAY(1);
194 		}
195 	}
196 
197 	return (0);
198 }
199 
200 static int
201 zy7_ehci_probe(device_t dev)
202 {
203 
204 	if (!ofw_bus_status_okay(dev))
205 		return (ENXIO);
206 
207 	if (!ofw_bus_is_compatible(dev, "xlnx,zy7_ehci"))
208 		return (ENXIO);
209 
210 	device_set_desc(dev, "Zynq-7000 EHCI USB 2.0 controller");
211 	return (0);
212 }
213 
214 static int zy7_ehci_detach(device_t dev);
215 
216 static int
217 zy7_ehci_attach(device_t dev)
218 {
219 	ehci_softc_t *sc = device_get_softc(dev);
220 	bus_space_handle_t bsh;
221 	int err, rid;
222 
223 	/* initialize some bus fields */
224 	sc->sc_bus.parent = dev;
225 	sc->sc_bus.devices = sc->sc_devices;
226 	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
227 	sc->sc_bus.dma_bits = 32;
228 
229 	/* get all DMA memory */
230 	if (usb_bus_mem_alloc_all(&sc->sc_bus,
231 	    USB_GET_DMA_TAG(dev), &ehci_iterate_hw_softc))
232 		return (ENOMEM);
233 
234 	/* Allocate memory. */
235 	rid = 0;
236 	sc->sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
237 					       &rid, RF_ACTIVE);
238 	if (sc->sc_io_res == NULL) {
239 		device_printf(dev, "Can't allocate memory");
240 		zy7_ehci_detach(dev);
241 		return (ENOMEM);
242 	}
243 
244 	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
245 	bsh = rman_get_bushandle(sc->sc_io_res);
246 	sc->sc_io_size = EHCI_REG_SIZE;
247 
248 	if (bus_space_subregion(sc->sc_io_tag, bsh, EHCI_REG_OFFSET,
249 				sc->sc_io_size, &sc->sc_io_hdl) != 0)
250 		panic("%s: unable to subregion USB host registers",
251 		      device_get_name(dev));
252 
253 	/* Allocate IRQ. */
254 	rid = 0;
255 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
256 						RF_ACTIVE);
257 	if (sc->sc_irq_res == NULL) {
258 		device_printf(dev, "Can't allocate IRQ\n");
259 		zy7_ehci_detach(dev);
260 		return (ENOMEM);
261 	}
262 
263 	/* Add USB device */
264 	sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
265 	if (!sc->sc_bus.bdev) {
266 		device_printf(dev, "Could not add USB device\n");
267 		zy7_ehci_detach(dev);
268 		return (ENXIO);
269 	}
270 	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
271 	device_set_desc(sc->sc_bus.bdev, "Zynq-7000 ehci USB 2.0 controller");
272 
273 	strcpy(sc->sc_vendor, "Xilinx"); /* or IP vendor? */
274 
275 	/* Activate the interrupt */
276 	err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
277 			     NULL, (driver_intr_t *)ehci_interrupt, sc,
278 			     &sc->sc_intr_hdl);
279 	if (err) {
280 		device_printf(dev, "Cannot setup IRQ\n");
281 		zy7_ehci_detach(dev);
282 		return (err);
283 	}
284 
285 	/* Customization. */
286 	sc->sc_flags |= EHCI_SCFLG_TT |	EHCI_SCFLG_NORESTERM;
287 	sc->sc_vendor_post_reset = zy7_ehci_post_reset;
288 	sc->sc_vendor_get_port_speed = ehci_get_port_speed_portsc;
289 
290 	/* Modify FIFO burst threshold from 2 to 8. */
291 	bus_space_write_4(sc->sc_io_tag, bsh,
292 			  ZY7_USB_TXFILLTUNING,
293 			  8 << ZY7_USB_TXFILLTUNING_TXFIFOTHRES_SHFT);
294 
295 	/* Handle PHY options. */
296 	if (zy7_phy_config(dev, sc->sc_io_tag, bsh) < 0) {
297 		device_printf(dev, "Cannot config phy!\n");
298 		zy7_ehci_detach(dev);
299 		return (EIO);
300 	}
301 
302 	/* Init ehci. */
303 	err = ehci_init(sc);
304 	if (!err) {
305 		sc->sc_flags |= EHCI_SCFLG_DONEINIT;
306 		err = device_probe_and_attach(sc->sc_bus.bdev);
307 	}
308 	if (err) {
309 		device_printf(dev, "USB init failed err=%d\n", err);
310 		zy7_ehci_detach(dev);
311 		return (err);
312 	}
313 
314 	return (0);
315 }
316 
317 static int
318 zy7_ehci_detach(device_t dev)
319 {
320 	ehci_softc_t *sc = device_get_softc(dev);
321 
322 	/* during module unload there are lots of children leftover */
323 	device_delete_children(dev);
324 
325 	if ((sc->sc_flags & EHCI_SCFLG_DONEINIT) != 0) {
326 		ehci_detach(sc);
327 		sc->sc_flags &= ~EHCI_SCFLG_DONEINIT;
328 	}
329 
330 	if (sc->sc_irq_res) {
331 		if (sc->sc_intr_hdl != NULL)
332 			bus_teardown_intr(dev, sc->sc_irq_res,
333 					  sc->sc_intr_hdl);
334 		bus_release_resource(dev, SYS_RES_IRQ,
335 			     rman_get_rid(sc->sc_irq_res), sc->sc_irq_res);
336 	}
337 
338 	if (sc->sc_io_res)
339 		bus_release_resource(dev, SYS_RES_MEMORY,
340 			     rman_get_rid(sc->sc_io_res), sc->sc_io_res);
341 	usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
342 
343 	return (0);
344 }
345 
346 static device_method_t ehci_methods[] = {
347 	/* Device interface */
348 	DEVMETHOD(device_probe,		zy7_ehci_probe),
349 	DEVMETHOD(device_attach, 	zy7_ehci_attach),
350 	DEVMETHOD(device_detach, 	zy7_ehci_detach),
351 	DEVMETHOD(device_suspend, 	bus_generic_suspend),
352 	DEVMETHOD(device_resume, 	bus_generic_resume),
353 	DEVMETHOD(device_shutdown, 	bus_generic_shutdown),
354 
355 	/* Bus interface */
356 	DEVMETHOD(bus_print_child, bus_generic_print_child),
357 
358 	DEVMETHOD_END
359 };
360 
361 static driver_t ehci_driver = {
362 	"ehci",
363 	ehci_methods,
364 	sizeof(struct ehci_softc),
365 };
366 
367 DRIVER_MODULE(zy7_ehci, simplebus, ehci_driver, NULL, NULL);
368 MODULE_DEPEND(zy7_ehci, usb, 1, 1, 1);
369