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