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