vf_ehci.c revision 302408
133965Sjdp/*-
233965Sjdp * Copyright (c) 2013 Ruslan Bukin <br@bsdpad.com>
333965Sjdp * All rights reserved.
433965Sjdp *
533965Sjdp * Redistribution and use in source and binary forms, with or without
633965Sjdp * modification, are permitted provided that the following conditions
733965Sjdp * are met:
833965Sjdp * 1. Redistributions of source code must retain the above copyright
933965Sjdp *    notice, this list of conditions and the following disclaimer.
1033965Sjdp * 2. Redistributions in binary form must reproduce the above copyright
1133965Sjdp *    notice, this list of conditions and the following disclaimer in the
1233965Sjdp *    documentation and/or other materials provided with the distribution.
1333965Sjdp *
1433965Sjdp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1533965Sjdp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1633965Sjdp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1733965Sjdp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1833965Sjdp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1933965Sjdp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2033965Sjdp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2133965Sjdp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2233965Sjdp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2333965Sjdp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2433965Sjdp * SUCH DAMAGE.
2533965Sjdp */
2633965Sjdp
2733965Sjdp/*
2833965Sjdp * Vybrid Family Universal Serial Bus (USB) Controller
2933965Sjdp * Chapter 44-45, Vybrid Reference Manual, Rev. 5, 07/2013
3033965Sjdp */
3133965Sjdp
3233965Sjdp#include <sys/cdefs.h>
3333965Sjdp__FBSDID("$FreeBSD: stable/11/sys/arm/freescale/vybrid/vf_ehci.c 294989 2016-01-28 14:11:59Z mmel $");
3433965Sjdp
3533965Sjdp#include "opt_bus.h"
3633965Sjdp
3733965Sjdp#include <sys/param.h>
3833965Sjdp#include <sys/systm.h>
3933965Sjdp#include <sys/kernel.h>
4033965Sjdp#include <sys/module.h>
4133965Sjdp#include <sys/bus.h>
4233965Sjdp#include <sys/condvar.h>
4333965Sjdp#include <sys/rman.h>
4433965Sjdp#include <sys/gpio.h>
4533965Sjdp
4633965Sjdp#include <dev/ofw/ofw_bus.h>
4733965Sjdp#include <dev/ofw/ofw_bus_subr.h>
4833965Sjdp
4933965Sjdp#include <dev/usb/usb.h>
5033965Sjdp#include <dev/usb/usbdi.h>
5133965Sjdp#include <dev/usb/usb_busdma.h>
5233965Sjdp#include <dev/usb/usb_process.h>
5333965Sjdp#include <dev/usb/usb_controller.h>
5433965Sjdp#include <dev/usb/usb_bus.h>
5533965Sjdp#include <dev/usb/controller/ehci.h>
5633965Sjdp#include <dev/usb/controller/ehcireg.h>
5733965Sjdp
5833965Sjdp#include <dev/fdt/fdt_common.h>
5933965Sjdp
6033965Sjdp#include <machine/bus.h>
6133965Sjdp#include <machine/resource.h>
6233965Sjdp
6333965Sjdp#include "gpio_if.h"
6433965Sjdp#include "opt_platform.h"
6533965Sjdp
6633965Sjdp#define	ENUTMILEVEL3	(1 << 15)
6733965Sjdp#define	ENUTMILEVEL2	(1 << 14)
6833965Sjdp
6933965Sjdp#define	GPIO_USB_PWR	134
7033965Sjdp
7133965Sjdp#define	USB_ID		0x000	/* Identification register */
7233965Sjdp#define	USB_HWGENERAL	0x004	/* Hardware General */
7333965Sjdp#define	USB_HWHOST	0x008	/* Host Hardware Parameters */
7433965Sjdp#define	USB_HWDEVICE	0x00C	/* Device Hardware Parameters */
7533965Sjdp#define	USB_HWTXBUF	0x010	/* TX Buffer Hardware Parameters */
7633965Sjdp#define	USB_HWRXBUF	0x014	/* RX Buffer Hardware Parameters */
7733965Sjdp#define	USB_HCSPARAMS	0x104	/* Host Controller Structural Parameters */
7833965Sjdp
7933965Sjdp#define	USBPHY_PWD		0x00	/* PHY Power-Down Register */
8033965Sjdp#define	USBPHY_PWD_SET		0x04	/* PHY Power-Down Register */
8133965Sjdp#define	USBPHY_PWD_CLR		0x08	/* PHY Power-Down Register */
8233965Sjdp#define	USBPHY_PWD_TOG		0x0C	/* PHY Power-Down Register */
8333965Sjdp#define	USBPHY_TX		0x10	/* PHY Transmitter Control Register */
8433965Sjdp#define	USBPHY_RX		0x20	/* PHY Receiver Control Register */
8533965Sjdp#define	USBPHY_RX_SET		0x24	/* PHY Receiver Control Register */
8633965Sjdp#define	USBPHY_RX_CLR		0x28	/* PHY Receiver Control Register */
8733965Sjdp#define	USBPHY_RX_TOG		0x2C	/* PHY Receiver Control Register */
8833965Sjdp#define	USBPHY_CTRL		0x30	/* PHY General Control Register */
8933965Sjdp#define	USBPHY_CTRL_SET		0x34	/* PHY General Control Register */
9033965Sjdp#define	USBPHY_CTRL_CLR		0x38	/* PHY General Control Register */
9133965Sjdp#define	USBPHY_CTRL_TOG		0x3C	/* PHY General Control Register */
9233965Sjdp#define	USBPHY_STATUS		0x40	/* PHY Status Register */
9333965Sjdp#define	USBPHY_DEBUG		0x50	/* PHY Debug Register */
9433965Sjdp#define	USBPHY_DEBUG_SET	0x54	/* PHY Debug Register */
9533965Sjdp#define	USBPHY_DEBUG_CLR	0x58	/* PHY Debug Register */
9633965Sjdp#define	USBPHY_DEBUG_TOG	0x5C	/* PHY Debug Register */
9733965Sjdp#define	USBPHY_DEBUG0_STATUS	0x60	/* UTMI Debug Status Register 0 */
9833965Sjdp#define	USBPHY_DEBUG1		0x70	/* UTMI Debug Status Register 1 */
9933965Sjdp#define	USBPHY_DEBUG1_SET	0x74	/* UTMI Debug Status Register 1 */
10033965Sjdp#define	USBPHY_DEBUG1_CLR	0x78	/* UTMI Debug Status Register 1 */
10133965Sjdp#define	USBPHY_DEBUG1_TOG	0x7C	/* UTMI Debug Status Register 1 */
10233965Sjdp#define	USBPHY_VERSION		0x80	/* UTMI RTL Version */
10333965Sjdp#define	USBPHY_IP		0x90	/* PHY IP Block Register */
10433965Sjdp#define	USBPHY_IP_SET		0x94	/* PHY IP Block Register */
10533965Sjdp#define	USBPHY_IP_CLR		0x98	/* PHY IP Block Register */
10633965Sjdp#define	USBPHY_IP_TOG		0x9C	/* PHY IP Block Register */
10733965Sjdp
10833965Sjdp#define	USBPHY_CTRL_SFTRST	(1U << 31)
10933965Sjdp#define	USBPHY_CTRL_CLKGATE	(1 << 30)
11033965Sjdp#define	USBPHY_DEBUG_CLKGATE	(1 << 30)
11133965Sjdp
11233965Sjdp#define	PHY_READ4(_sc, _reg)		\
113	bus_space_read_4(_sc->bst_phy, _sc->bsh_phy, _reg)
114#define	PHY_WRITE4(_sc, _reg, _val)	\
115	bus_space_write_4(_sc->bst_phy, _sc->bsh_phy, _reg, _val)
116
117#define	USBC_READ4(_sc, _reg)		\
118	bus_space_read_4(_sc->bst_usbc, _sc->bsh_usbc, _reg)
119#define	USBC_WRITE4(_sc, _reg, _val)	\
120	bus_space_write_4(_sc->bst_usbc, _sc->bsh_usbc, _reg, _val)
121
122/* Forward declarations */
123static int	vybrid_ehci_attach(device_t dev);
124static int	vybrid_ehci_detach(device_t dev);
125static int	vybrid_ehci_probe(device_t dev);
126
127struct vybrid_ehci_softc {
128	ehci_softc_t		base;
129	device_t		dev;
130	struct resource		*res[6];
131	bus_space_tag_t		bst_phy;
132	bus_space_handle_t      bsh_phy;
133	bus_space_tag_t		bst_usbc;
134	bus_space_handle_t      bsh_usbc;
135};
136
137static struct resource_spec vybrid_ehci_spec[] = {
138	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
139	{ SYS_RES_MEMORY,	1,	RF_ACTIVE },
140	{ SYS_RES_MEMORY,	2,	RF_ACTIVE },
141	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
142	{ -1, 0 }
143};
144
145static device_method_t ehci_methods[] = {
146	/* Device interface */
147	DEVMETHOD(device_probe, vybrid_ehci_probe),
148	DEVMETHOD(device_attach, vybrid_ehci_attach),
149	DEVMETHOD(device_detach, vybrid_ehci_detach),
150	DEVMETHOD(device_suspend, bus_generic_suspend),
151	DEVMETHOD(device_resume, bus_generic_resume),
152	DEVMETHOD(device_shutdown, bus_generic_shutdown),
153
154	/* Bus interface */
155	DEVMETHOD(bus_print_child, bus_generic_print_child),
156
157	{ 0, 0 }
158};
159
160/* kobj_class definition */
161static driver_t ehci_driver = {
162	"ehci",
163	ehci_methods,
164	sizeof(ehci_softc_t)
165};
166
167static devclass_t ehci_devclass;
168
169DRIVER_MODULE(ehci, simplebus, ehci_driver, ehci_devclass, 0, 0);
170MODULE_DEPEND(ehci, usb, 1, 1, 1);
171
172static void
173vybrid_ehci_post_reset(struct ehci_softc *ehci_softc)
174{
175	uint32_t usbmode;
176
177	/* Force HOST mode */
178	usbmode = EOREAD4(ehci_softc, EHCI_USBMODE_NOLPM);
179	usbmode &= ~EHCI_UM_CM;
180	usbmode |= EHCI_UM_CM_HOST;
181	EOWRITE4(ehci_softc, EHCI_USBMODE_NOLPM, usbmode);
182}
183
184/*
185 * Public methods
186 */
187static int
188vybrid_ehci_probe(device_t dev)
189{
190
191	if (!ofw_bus_status_okay(dev))
192		return (ENXIO);
193
194	if (ofw_bus_is_compatible(dev, "fsl,mvf600-usb-ehci") == 0)
195		return (ENXIO);
196
197	device_set_desc(dev, "Vybrid Family integrated USB controller");
198	return (BUS_PROBE_DEFAULT);
199}
200
201static int
202phy_init(struct vybrid_ehci_softc *esc)
203{
204	device_t sc_gpio_dev;
205	int reg;
206
207	/* Reset phy */
208	reg = PHY_READ4(esc, USBPHY_CTRL);
209	reg |= (USBPHY_CTRL_SFTRST);
210	PHY_WRITE4(esc, USBPHY_CTRL, reg);
211
212	/* Minimum reset time */
213	DELAY(10000);
214
215	reg &= ~(USBPHY_CTRL_SFTRST | USBPHY_CTRL_CLKGATE);
216	PHY_WRITE4(esc, USBPHY_CTRL, reg);
217
218	reg = (ENUTMILEVEL2 | ENUTMILEVEL3);
219	PHY_WRITE4(esc, USBPHY_CTRL_SET, reg);
220
221	/* Get the GPIO device, we need this to give power to USB */
222	sc_gpio_dev = devclass_get_device(devclass_find("gpio"), 0);
223	if (sc_gpio_dev == NULL) {
224		device_printf(esc->dev, "Error: failed to get the GPIO dev\n");
225		return (1);
226	}
227
228	/* Give power to USB */
229	GPIO_PIN_SETFLAGS(sc_gpio_dev, GPIO_USB_PWR, GPIO_PIN_OUTPUT);
230	GPIO_PIN_SET(sc_gpio_dev, GPIO_USB_PWR, GPIO_PIN_HIGH);
231
232	/* Power up PHY */
233	PHY_WRITE4(esc, USBPHY_PWD, 0x00);
234
235	/* Ungate clocks */
236	reg = PHY_READ4(esc, USBPHY_DEBUG);
237	reg &= ~(USBPHY_DEBUG_CLKGATE);
238	PHY_WRITE4(esc, USBPHY_DEBUG, reg);
239
240#if 0
241	printf("USBPHY_CTRL == 0x%08x\n",
242	    PHY_READ4(esc, USBPHY_CTRL));
243	printf("USBPHY_IP == 0x%08x\n",
244	    PHY_READ4(esc, USBPHY_IP));
245	printf("USBPHY_STATUS == 0x%08x\n",
246	    PHY_READ4(esc, USBPHY_STATUS));
247	printf("USBPHY_DEBUG == 0x%08x\n",
248	    PHY_READ4(esc, USBPHY_DEBUG));
249	printf("USBPHY_DEBUG0_STATUS == 0x%08x\n",
250	    PHY_READ4(esc, USBPHY_DEBUG0_STATUS));
251	printf("USBPHY_DEBUG1 == 0x%08x\n",
252	    PHY_READ4(esc, USBPHY_DEBUG1));
253#endif
254
255	return (0);
256}
257
258static int
259vybrid_ehci_attach(device_t dev)
260{
261	struct vybrid_ehci_softc *esc;
262	ehci_softc_t *sc;
263	bus_space_handle_t bsh;
264	int err;
265	int reg;
266
267	esc = device_get_softc(dev);
268	esc->dev = dev;
269
270	sc = &esc->base;
271	sc->sc_bus.parent = dev;
272	sc->sc_bus.devices = sc->sc_devices;
273	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
274	sc->sc_bus.dma_bits = 32;
275
276	if (bus_alloc_resources(dev, vybrid_ehci_spec, esc->res)) {
277		device_printf(dev, "could not allocate resources\n");
278		return (ENXIO);
279	}
280
281	/* EHCI registers */
282	sc->sc_io_tag = rman_get_bustag(esc->res[0]);
283	bsh = rman_get_bushandle(esc->res[0]);
284	sc->sc_io_size = rman_get_size(esc->res[0]);
285
286	esc->bst_usbc = rman_get_bustag(esc->res[1]);
287	esc->bsh_usbc = rman_get_bushandle(esc->res[1]);
288
289	esc->bst_phy = rman_get_bustag(esc->res[2]);
290	esc->bsh_phy = rman_get_bushandle(esc->res[2]);
291
292	/* get all DMA memory */
293	if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(dev),
294		&ehci_iterate_hw_softc))
295		return (ENXIO);
296
297#if 0
298	printf("USBx_HCSPARAMS is 0x%08x\n",
299	    bus_space_read_4(sc->sc_io_tag, bsh, USB_HCSPARAMS));
300	printf("USB_ID == 0x%08x\n",
301	    bus_space_read_4(sc->sc_io_tag, bsh, USB_ID));
302	printf("USB_HWGENERAL == 0x%08x\n",
303	    bus_space_read_4(sc->sc_io_tag, bsh, USB_HWGENERAL));
304	printf("USB_HWHOST == 0x%08x\n",
305	    bus_space_read_4(sc->sc_io_tag, bsh, USB_HWHOST));
306	printf("USB_HWDEVICE == 0x%08x\n",
307	    bus_space_read_4(sc->sc_io_tag, bsh, USB_HWDEVICE));
308	printf("USB_HWTXBUF == 0x%08x\n",
309	    bus_space_read_4(sc->sc_io_tag, bsh, USB_HWTXBUF));
310	printf("USB_HWRXBUF == 0x%08x\n",
311	    bus_space_read_4(sc->sc_io_tag, bsh, USB_HWRXBUF));
312#endif
313
314	if (phy_init(esc)) {
315		device_printf(dev, "Could not setup PHY\n");
316		return (1);
317	}
318
319	/*
320	 * Set handle to USB related registers subregion used by
321	 * generic EHCI driver.
322	 */
323	err = bus_space_subregion(sc->sc_io_tag, bsh, 0x100,
324	    sc->sc_io_size, &sc->sc_io_hdl);
325	if (err != 0)
326		return (ENXIO);
327
328	/* Setup interrupt handler */
329	err = bus_setup_intr(dev, esc->res[3], INTR_TYPE_BIO | INTR_MPSAFE,
330	    NULL, (driver_intr_t *)ehci_interrupt, sc,
331	    &sc->sc_intr_hdl);
332	if (err) {
333		device_printf(dev, "Could not setup irq, "
334		    "%d\n", err);
335		return (1);
336	}
337
338	/* Add USB device */
339	sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
340	if (!sc->sc_bus.bdev) {
341		device_printf(dev, "Could not add USB device\n");
342		err = bus_teardown_intr(dev, esc->res[5],
343		    sc->sc_intr_hdl);
344		if (err)
345			device_printf(dev, "Could not tear down irq,"
346			    " %d\n", err);
347		return (1);
348	}
349	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
350
351	strlcpy(sc->sc_vendor, "Freescale", sizeof(sc->sc_vendor));
352
353	/* Set host mode */
354	reg = bus_space_read_4(sc->sc_io_tag, sc->sc_io_hdl, 0xA8);
355	reg |= 0x3;
356	bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl, 0xA8, reg);
357
358	/* Set flags  and callbacks*/
359	sc->sc_flags |= EHCI_SCFLG_TT | EHCI_SCFLG_NORESTERM;
360	sc->sc_vendor_post_reset = vybrid_ehci_post_reset;
361	sc->sc_vendor_get_port_speed = ehci_get_port_speed_portsc;
362
363	err = ehci_init(sc);
364	if (!err) {
365		sc->sc_flags |= EHCI_SCFLG_DONEINIT;
366		err = device_probe_and_attach(sc->sc_bus.bdev);
367	} else {
368		device_printf(dev, "USB init failed err=%d\n", err);
369
370		device_delete_child(dev, sc->sc_bus.bdev);
371		sc->sc_bus.bdev = NULL;
372
373		err = bus_teardown_intr(dev, esc->res[5],
374		    sc->sc_intr_hdl);
375		if (err)
376			device_printf(dev, "Could not tear down irq,"
377			    " %d\n", err);
378		return (1);
379	}
380	return (0);
381}
382
383static int
384vybrid_ehci_detach(device_t dev)
385{
386	struct vybrid_ehci_softc *esc;
387	ehci_softc_t *sc;
388	int err;
389
390	esc = device_get_softc(dev);
391	sc = &esc->base;
392
393	if (sc->sc_flags & EHCI_SCFLG_DONEINIT)
394		return (0);
395
396	/*
397	 * only call ehci_detach() after ehci_init()
398	 */
399	if (sc->sc_flags & EHCI_SCFLG_DONEINIT) {
400		ehci_detach(sc);
401		sc->sc_flags &= ~EHCI_SCFLG_DONEINIT;
402	}
403
404	/*
405	 * Disable interrupts that might have been switched on in
406	 * ehci_init.
407	 */
408	if (sc->sc_io_tag && sc->sc_io_hdl)
409		bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl,
410		    EHCI_USBINTR, 0);
411
412	if (esc->res[5] && sc->sc_intr_hdl) {
413		err = bus_teardown_intr(dev, esc->res[5],
414		    sc->sc_intr_hdl);
415		if (err) {
416			device_printf(dev, "Could not tear down irq,"
417			    " %d\n", err);
418			return (err);
419		}
420		sc->sc_intr_hdl = NULL;
421	}
422
423	if (sc->sc_bus.bdev) {
424		device_delete_child(dev, sc->sc_bus.bdev);
425		sc->sc_bus.bdev = NULL;
426	}
427
428	/* During module unload there are lots of children leftover */
429	device_delete_children(dev);
430
431	bus_release_resources(dev, vybrid_ehci_spec, esc->res);
432
433	return (0);
434}
435