xref: /freebsd/sys/arm/freescale/imx/imx6_usbphy.c (revision 8148f4f3d5627532262aa8f56a14a2f9b2230719)
1c8c16b3dSIan Lepore /*-
2c8c16b3dSIan Lepore  * Copyright (c) 2013 Ian Lepore <ian@freebsd.org>
3c8c16b3dSIan Lepore  * All rights reserved.
4c8c16b3dSIan Lepore  *
5c8c16b3dSIan Lepore  * Redistribution and use in source and binary forms, with or without
6c8c16b3dSIan Lepore  * modification, are permitted provided that the following conditions
7c8c16b3dSIan Lepore  * are met:
8c8c16b3dSIan Lepore  * 1. Redistributions of source code must retain the above copyright
9c8c16b3dSIan Lepore  *    notice, this list of conditions and the following disclaimer.
10c8c16b3dSIan Lepore  * 2. Redistributions in binary form must reproduce the above copyright
11c8c16b3dSIan Lepore  *    notice, this list of conditions and the following disclaimer in the
12c8c16b3dSIan Lepore  *    documentation and/or other materials provided with the distribution.
13c8c16b3dSIan Lepore  *
14c8c16b3dSIan Lepore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15c8c16b3dSIan Lepore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16c8c16b3dSIan Lepore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17c8c16b3dSIan Lepore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18c8c16b3dSIan Lepore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19c8c16b3dSIan Lepore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20c8c16b3dSIan Lepore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21c8c16b3dSIan Lepore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22c8c16b3dSIan Lepore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23c8c16b3dSIan Lepore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24c8c16b3dSIan Lepore  * SUCH DAMAGE.
25c8c16b3dSIan Lepore  */
26c8c16b3dSIan Lepore 
27c8c16b3dSIan Lepore #include <sys/cdefs.h>
28c8c16b3dSIan Lepore __FBSDID("$FreeBSD$");
29c8c16b3dSIan Lepore 
30c8c16b3dSIan Lepore /*
31c8c16b3dSIan Lepore  * USBPHY driver for Freescale i.MX6 family of SoCs.
32c8c16b3dSIan Lepore  */
33c8c16b3dSIan Lepore 
34c8c16b3dSIan Lepore #include "opt_bus.h"
35c8c16b3dSIan Lepore 
36c8c16b3dSIan Lepore #include <sys/param.h>
37c8c16b3dSIan Lepore #include <sys/systm.h>
38c8c16b3dSIan Lepore #include <sys/kernel.h>
39c8c16b3dSIan Lepore #include <sys/module.h>
40c8c16b3dSIan Lepore #include <sys/bus.h>
41c8c16b3dSIan Lepore #include <sys/rman.h>
42c8c16b3dSIan Lepore 
43c8c16b3dSIan Lepore #include <dev/ofw/ofw_bus.h>
44c8c16b3dSIan Lepore #include <dev/ofw/ofw_bus_subr.h>
45c8c16b3dSIan Lepore 
46c8c16b3dSIan Lepore #include <machine/bus.h>
47c8c16b3dSIan Lepore 
48a7fa939bSIan Lepore #include <arm/freescale/imx/imx_ccmvar.h>
49c8c16b3dSIan Lepore #include <arm/freescale/imx/imx6_anatopreg.h>
50c8c16b3dSIan Lepore #include <arm/freescale/imx/imx6_anatopvar.h>
51c8c16b3dSIan Lepore 
52c8c16b3dSIan Lepore /*
53c8c16b3dSIan Lepore  * Hardware register defines.
54c8c16b3dSIan Lepore  */
55c8c16b3dSIan Lepore #define	PWD_REG				0x0000
56c8c16b3dSIan Lepore #define	CTRL_STATUS_REG			0x0030
57c8c16b3dSIan Lepore #define	CTRL_SET_REG			0x0034
58c8c16b3dSIan Lepore #define	CTRL_CLR_REG			0x0038
59c8c16b3dSIan Lepore #define	CTRL_TOGGLE_REG			0x003c
607a22215cSEitan Adler #define	  CTRL_SFTRST			  (1U << 31)
61c8c16b3dSIan Lepore #define	  CTRL_CLKGATE			  (1 << 30)
62c8c16b3dSIan Lepore #define	  CTRL_ENUTMILEVEL3		  (1 << 15)
63c8c16b3dSIan Lepore #define	  CTRL_ENUTMILEVEL2		  (1 << 14)
64c8c16b3dSIan Lepore 
65c8c16b3dSIan Lepore struct usbphy_softc {
66c8c16b3dSIan Lepore 	device_t	dev;
67c8c16b3dSIan Lepore 	struct resource	*mem_res;
68c8c16b3dSIan Lepore 	u_int		phy_num;
69c8c16b3dSIan Lepore };
70c8c16b3dSIan Lepore 
71c8c16b3dSIan Lepore static int
72c8c16b3dSIan Lepore usbphy_detach(device_t dev)
73c8c16b3dSIan Lepore {
74c8c16b3dSIan Lepore 	struct usbphy_softc *sc;
75c8c16b3dSIan Lepore 
76c8c16b3dSIan Lepore 	sc = device_get_softc(dev);
77c8c16b3dSIan Lepore 
78c8c16b3dSIan Lepore 	if (sc->mem_res != NULL)
79c8c16b3dSIan Lepore 		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
80c8c16b3dSIan Lepore 
81c8c16b3dSIan Lepore 	return (0);
82c8c16b3dSIan Lepore }
83c8c16b3dSIan Lepore 
84c8c16b3dSIan Lepore static int
85c8c16b3dSIan Lepore usbphy_attach(device_t dev)
86c8c16b3dSIan Lepore {
87c8c16b3dSIan Lepore 	struct usbphy_softc *sc;
88c8c16b3dSIan Lepore 	int err, regoff, rid;
89c8c16b3dSIan Lepore 
90c8c16b3dSIan Lepore 	sc = device_get_softc(dev);
91c8c16b3dSIan Lepore 	err = 0;
92c8c16b3dSIan Lepore 
93c8c16b3dSIan Lepore 	/* Allocate bus_space resources. */
94c8c16b3dSIan Lepore 	rid = 0;
95c8c16b3dSIan Lepore 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
96c8c16b3dSIan Lepore 	    RF_ACTIVE);
97c8c16b3dSIan Lepore 	if (sc->mem_res == NULL) {
98c8c16b3dSIan Lepore 		device_printf(dev, "Cannot allocate memory resources\n");
99c8c16b3dSIan Lepore 		err = ENXIO;
100c8c16b3dSIan Lepore 		goto out;
101c8c16b3dSIan Lepore 	}
102c8c16b3dSIan Lepore 
103c8c16b3dSIan Lepore 	/*
104c8c16b3dSIan Lepore 	 * XXX Totally lame way to get the unit number (but not quite as lame as
105c8c16b3dSIan Lepore 	 * adding an ad-hoc property to the fdt data).  This works as long as
106c8c16b3dSIan Lepore 	 * this driver is used for imx6 only.
107c8c16b3dSIan Lepore 	 */
108c8c16b3dSIan Lepore 	const uint32_t PWD_PHY1_REG_PHYSADDR = 0x020c9000;
109c8c16b3dSIan Lepore 	if (BUS_SPACE_PHYSADDR(sc->mem_res, 0) == PWD_PHY1_REG_PHYSADDR) {
110c8c16b3dSIan Lepore 		sc->phy_num = 0;
111c8c16b3dSIan Lepore 		regoff = 0;
112c8c16b3dSIan Lepore 	} else {
113c8c16b3dSIan Lepore 		sc->phy_num = 1;
114c8c16b3dSIan Lepore 		regoff = 0x60;
115c8c16b3dSIan Lepore 	}
116c8c16b3dSIan Lepore 
117c8c16b3dSIan Lepore 	/*
118c8c16b3dSIan Lepore 	 * Based on a note in the u-boot source code, disable charger detection
119c8c16b3dSIan Lepore 	 * to avoid degrading the differential signaling on the DP line.  Note
120c8c16b3dSIan Lepore 	 * that this disables (by design) both charger detection and contact
121c8c16b3dSIan Lepore 	 * detection, because of the screwball mix of active-high and active-low
122c8c16b3dSIan Lepore 	 * bits in this register.
123c8c16b3dSIan Lepore 	 */
124c8c16b3dSIan Lepore 	imx6_anatop_write_4(IMX6_ANALOG_USB1_CHRG_DETECT + regoff,
125c8c16b3dSIan Lepore 	    IMX6_ANALOG_USB_CHRG_DETECT_N_ENABLE |
126c8c16b3dSIan Lepore 	    IMX6_ANALOG_USB_CHRG_DETECT_N_CHK_CHRG);
127c8c16b3dSIan Lepore 
128c8c16b3dSIan Lepore 	imx6_anatop_write_4(IMX6_ANALOG_USB1_CHRG_DETECT + regoff,
129c8c16b3dSIan Lepore 	    IMX6_ANALOG_USB_CHRG_DETECT_N_ENABLE |
130c8c16b3dSIan Lepore 	    IMX6_ANALOG_USB_CHRG_DETECT_N_CHK_CHRG);
131c8c16b3dSIan Lepore 
132c8c16b3dSIan Lepore 	/* XXX Configure the overcurrent detection here. */
133c8c16b3dSIan Lepore 
134c8c16b3dSIan Lepore 	/*
135c8c16b3dSIan Lepore 	 * Turn on the phy clocks.
136c8c16b3dSIan Lepore 	 */
137c8c16b3dSIan Lepore 	imx_ccm_usbphy_enable(dev);
138c8c16b3dSIan Lepore 
139c8c16b3dSIan Lepore 	/*
140c8c16b3dSIan Lepore 	 * Set the software reset bit, then clear both it and the clock gate bit
141c8c16b3dSIan Lepore 	 * to bring the device out of reset with the clock running.
142c8c16b3dSIan Lepore 	 */
143c8c16b3dSIan Lepore 	bus_write_4(sc->mem_res, CTRL_SET_REG, CTRL_SFTRST);
144c8c16b3dSIan Lepore 	bus_write_4(sc->mem_res, CTRL_CLR_REG, CTRL_SFTRST | CTRL_CLKGATE);
145c8c16b3dSIan Lepore 
146*8148f4f3SIan Lepore 	/* Set UTMI+ level 2+3 bits to enable low and full speed devices. */
147*8148f4f3SIan Lepore 	bus_write_4(sc->mem_res, CTRL_SET_REG,
148*8148f4f3SIan Lepore 	    CTRL_ENUTMILEVEL2 | CTRL_ENUTMILEVEL3);
149*8148f4f3SIan Lepore 
150c8c16b3dSIan Lepore 	/* Power up: clear all bits in the powerdown register. */
151c8c16b3dSIan Lepore 	bus_write_4(sc->mem_res, PWD_REG, 0);
152c8c16b3dSIan Lepore 
153c8c16b3dSIan Lepore 	err = 0;
154c8c16b3dSIan Lepore 
155c8c16b3dSIan Lepore out:
156c8c16b3dSIan Lepore 
157c8c16b3dSIan Lepore 	if (err != 0)
158c8c16b3dSIan Lepore 		usbphy_detach(dev);
159c8c16b3dSIan Lepore 
160c8c16b3dSIan Lepore 	return (err);
161c8c16b3dSIan Lepore }
162c8c16b3dSIan Lepore 
163c8c16b3dSIan Lepore static int
164c8c16b3dSIan Lepore usbphy_probe(device_t dev)
165c8c16b3dSIan Lepore {
166c8c16b3dSIan Lepore 
167add35ed5SIan Lepore 	if (!ofw_bus_status_okay(dev))
168add35ed5SIan Lepore 		return (ENXIO);
169add35ed5SIan Lepore 
170c8c16b3dSIan Lepore 	if (ofw_bus_is_compatible(dev, "fsl,imx6q-usbphy") == 0)
171c8c16b3dSIan Lepore 		return (ENXIO);
172c8c16b3dSIan Lepore 
173c8c16b3dSIan Lepore 	device_set_desc(dev, "Freescale i.MX6 USB PHY");
174c8c16b3dSIan Lepore 
175c8c16b3dSIan Lepore 	return (BUS_PROBE_DEFAULT);
176c8c16b3dSIan Lepore }
177c8c16b3dSIan Lepore 
178c8c16b3dSIan Lepore static device_method_t usbphy_methods[] = {
179c8c16b3dSIan Lepore 	/* Device interface */
180c8c16b3dSIan Lepore 	DEVMETHOD(device_probe,  usbphy_probe),
181c8c16b3dSIan Lepore 	DEVMETHOD(device_attach, usbphy_attach),
182c8c16b3dSIan Lepore 	DEVMETHOD(device_detach, usbphy_detach),
183c8c16b3dSIan Lepore 
184c8c16b3dSIan Lepore 	DEVMETHOD_END
185c8c16b3dSIan Lepore };
186c8c16b3dSIan Lepore 
187c8c16b3dSIan Lepore static driver_t usbphy_driver = {
188c8c16b3dSIan Lepore 	"usbphy",
189c8c16b3dSIan Lepore 	usbphy_methods,
190c8c16b3dSIan Lepore 	sizeof(struct usbphy_softc)
191c8c16b3dSIan Lepore };
192c8c16b3dSIan Lepore 
193c8c16b3dSIan Lepore static devclass_t usbphy_devclass;
194c8c16b3dSIan Lepore 
195c8c16b3dSIan Lepore DRIVER_MODULE(usbphy, simplebus, usbphy_driver, usbphy_devclass, 0, 0);
196c8c16b3dSIan Lepore 
197