2 * Copyright (c) 2013 Ian Lepore <ian@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * USBPHY driver for Freescale i.MX6 family of SoCs.
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
46 #include <machine/bus.h>
48 #include <arm/freescale/imx/imx_ccmvar.h>
49 #include <arm/freescale/imx/imx6_anatopreg.h>
50 #include <arm/freescale/imx/imx6_anatopvar.h>
53 * Hardware register defines.
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)
67 struct resource *mem_res;
72 usbphy_detach(device_t dev)
74 struct usbphy_softc *sc;
76 sc = device_get_softc(dev);
78 if (sc->mem_res != NULL)
79 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
85 usbphy_attach(device_t dev)
87 struct usbphy_softc *sc;
90 sc = device_get_softc(dev);
93 /* Allocate bus_space resources. */
95 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
97 if (sc->mem_res == NULL) {
98 device_printf(dev, "Cannot allocate memory resources\n");
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.
108 const uint32_t PWD_PHY1_REG_PHYSADDR = 0x020c9000;
109 if (BUS_SPACE_PHYSADDR(sc->mem_res, 0) == PWD_PHY1_REG_PHYSADDR) {
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.
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);
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);
132 /* XXX Configure the overcurrent detection here. */
135 * Turn on the phy clocks.
137 imx_ccm_usbphy_enable(dev);
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.
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);
146 /* Power up: clear all bits in the powerdown register. */
147 bus_write_4(sc->mem_res, PWD_REG, 0);
160 usbphy_probe(device_t dev)
163 if (!ofw_bus_status_okay(dev))
166 if (ofw_bus_is_compatible(dev, "fsl,imx6q-usbphy") == 0)
169 device_set_desc(dev, "Freescale i.MX6 USB PHY");
171 return (BUS_PROBE_DEFAULT);
174 static device_method_t usbphy_methods[] = {
175 /* Device interface */
176 DEVMETHOD(device_probe, usbphy_probe),
177 DEVMETHOD(device_attach, usbphy_attach),
178 DEVMETHOD(device_detach, usbphy_detach),
183 static driver_t usbphy_driver = {
186 sizeof(struct usbphy_softc)
189 static devclass_t usbphy_devclass;
191 DRIVER_MODULE(usbphy, simplebus, usbphy_driver, usbphy_devclass, 0, 0);