1c8c16b3dSIan Lepore /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3af3dc4a7SPedro 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 /*
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
71b8255b91SIan Lepore static struct ofw_compat_data compat_data[] = {
72b8255b91SIan Lepore {"fsl,imx6q-usbphy", true},
73b8255b91SIan Lepore {"fsl,imx6ul-usbphy", true},
74b8255b91SIan Lepore {NULL, false}
75b8255b91SIan Lepore };
76b8255b91SIan Lepore
77c8c16b3dSIan Lepore static int
usbphy_detach(device_t dev)78c8c16b3dSIan Lepore usbphy_detach(device_t dev)
79c8c16b3dSIan Lepore {
80c8c16b3dSIan Lepore struct usbphy_softc *sc;
81c8c16b3dSIan Lepore
82c8c16b3dSIan Lepore sc = device_get_softc(dev);
83c8c16b3dSIan Lepore
84c8c16b3dSIan Lepore if (sc->mem_res != NULL)
85c8c16b3dSIan Lepore bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
86c8c16b3dSIan Lepore
87c8c16b3dSIan Lepore return (0);
88c8c16b3dSIan Lepore }
89c8c16b3dSIan Lepore
90c8c16b3dSIan Lepore static int
usbphy_attach(device_t dev)91c8c16b3dSIan Lepore usbphy_attach(device_t dev)
92c8c16b3dSIan Lepore {
93c8c16b3dSIan Lepore struct usbphy_softc *sc;
94c8c16b3dSIan Lepore int err, regoff, rid;
95c8c16b3dSIan Lepore
96c8c16b3dSIan Lepore sc = device_get_softc(dev);
97c8c16b3dSIan Lepore err = 0;
98c8c16b3dSIan Lepore
99c8c16b3dSIan Lepore /* Allocate bus_space resources. */
100c8c16b3dSIan Lepore rid = 0;
101c8c16b3dSIan Lepore sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
102c8c16b3dSIan Lepore RF_ACTIVE);
103c8c16b3dSIan Lepore if (sc->mem_res == NULL) {
104c8c16b3dSIan Lepore device_printf(dev, "Cannot allocate memory resources\n");
105c8c16b3dSIan Lepore err = ENXIO;
106c8c16b3dSIan Lepore goto out;
107c8c16b3dSIan Lepore }
108c8c16b3dSIan Lepore
109c8c16b3dSIan Lepore /*
110c8c16b3dSIan Lepore * XXX Totally lame way to get the unit number (but not quite as lame as
111c8c16b3dSIan Lepore * adding an ad-hoc property to the fdt data). This works as long as
112c8c16b3dSIan Lepore * this driver is used for imx6 only.
113c8c16b3dSIan Lepore */
114c8c16b3dSIan Lepore const uint32_t PWD_PHY1_REG_PHYSADDR = 0x020c9000;
115c8c16b3dSIan Lepore if (BUS_SPACE_PHYSADDR(sc->mem_res, 0) == PWD_PHY1_REG_PHYSADDR) {
116c8c16b3dSIan Lepore sc->phy_num = 0;
117c8c16b3dSIan Lepore regoff = 0;
118c8c16b3dSIan Lepore } else {
119c8c16b3dSIan Lepore sc->phy_num = 1;
120c8c16b3dSIan Lepore regoff = 0x60;
121c8c16b3dSIan Lepore }
122c8c16b3dSIan Lepore
123c8c16b3dSIan Lepore /*
124c8c16b3dSIan Lepore * Based on a note in the u-boot source code, disable charger detection
125c8c16b3dSIan Lepore * to avoid degrading the differential signaling on the DP line. Note
126c8c16b3dSIan Lepore * that this disables (by design) both charger detection and contact
127c8c16b3dSIan Lepore * detection, because of the screwball mix of active-high and active-low
128c8c16b3dSIan Lepore * bits in this register.
129c8c16b3dSIan Lepore */
130c8c16b3dSIan Lepore imx6_anatop_write_4(IMX6_ANALOG_USB1_CHRG_DETECT + regoff,
131c8c16b3dSIan Lepore IMX6_ANALOG_USB_CHRG_DETECT_N_ENABLE |
132c8c16b3dSIan Lepore IMX6_ANALOG_USB_CHRG_DETECT_N_CHK_CHRG);
133c8c16b3dSIan Lepore
134c8c16b3dSIan Lepore imx6_anatop_write_4(IMX6_ANALOG_USB1_CHRG_DETECT + regoff,
135c8c16b3dSIan Lepore IMX6_ANALOG_USB_CHRG_DETECT_N_ENABLE |
136c8c16b3dSIan Lepore IMX6_ANALOG_USB_CHRG_DETECT_N_CHK_CHRG);
137c8c16b3dSIan Lepore
138c8c16b3dSIan Lepore /* XXX Configure the overcurrent detection here. */
139c8c16b3dSIan Lepore
140c8c16b3dSIan Lepore /*
141c8c16b3dSIan Lepore * Turn on the phy clocks.
142c8c16b3dSIan Lepore */
143c8c16b3dSIan Lepore imx_ccm_usbphy_enable(dev);
144c8c16b3dSIan Lepore
145c8c16b3dSIan Lepore /*
146c8c16b3dSIan Lepore * Set the software reset bit, then clear both it and the clock gate bit
147c8c16b3dSIan Lepore * to bring the device out of reset with the clock running.
148c8c16b3dSIan Lepore */
149c8c16b3dSIan Lepore bus_write_4(sc->mem_res, CTRL_SET_REG, CTRL_SFTRST);
150c8c16b3dSIan Lepore bus_write_4(sc->mem_res, CTRL_CLR_REG, CTRL_SFTRST | CTRL_CLKGATE);
151c8c16b3dSIan Lepore
1528148f4f3SIan Lepore /* Set UTMI+ level 2+3 bits to enable low and full speed devices. */
1538148f4f3SIan Lepore bus_write_4(sc->mem_res, CTRL_SET_REG,
1548148f4f3SIan Lepore CTRL_ENUTMILEVEL2 | CTRL_ENUTMILEVEL3);
1558148f4f3SIan Lepore
156c8c16b3dSIan Lepore /* Power up: clear all bits in the powerdown register. */
157c8c16b3dSIan Lepore bus_write_4(sc->mem_res, PWD_REG, 0);
158c8c16b3dSIan Lepore
159c8c16b3dSIan Lepore err = 0;
160c8c16b3dSIan Lepore
161c8c16b3dSIan Lepore out:
162c8c16b3dSIan Lepore
163c8c16b3dSIan Lepore if (err != 0)
164c8c16b3dSIan Lepore usbphy_detach(dev);
165c8c16b3dSIan Lepore
166c8c16b3dSIan Lepore return (err);
167c8c16b3dSIan Lepore }
168c8c16b3dSIan Lepore
169c8c16b3dSIan Lepore static int
usbphy_probe(device_t dev)170c8c16b3dSIan Lepore usbphy_probe(device_t dev)
171c8c16b3dSIan Lepore {
172c8c16b3dSIan Lepore
173add35ed5SIan Lepore if (!ofw_bus_status_okay(dev))
174add35ed5SIan Lepore return (ENXIO);
175add35ed5SIan Lepore
176b8255b91SIan Lepore if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
177c8c16b3dSIan Lepore return (ENXIO);
178c8c16b3dSIan Lepore
179c8c16b3dSIan Lepore device_set_desc(dev, "Freescale i.MX6 USB PHY");
180c8c16b3dSIan Lepore
181c8c16b3dSIan Lepore return (BUS_PROBE_DEFAULT);
182c8c16b3dSIan Lepore }
183c8c16b3dSIan Lepore
184c8c16b3dSIan Lepore static device_method_t usbphy_methods[] = {
185c8c16b3dSIan Lepore /* Device interface */
186c8c16b3dSIan Lepore DEVMETHOD(device_probe, usbphy_probe),
187c8c16b3dSIan Lepore DEVMETHOD(device_attach, usbphy_attach),
188c8c16b3dSIan Lepore DEVMETHOD(device_detach, usbphy_detach),
189c8c16b3dSIan Lepore
190c8c16b3dSIan Lepore DEVMETHOD_END
191c8c16b3dSIan Lepore };
192c8c16b3dSIan Lepore
193c8c16b3dSIan Lepore static driver_t usbphy_driver = {
194c8c16b3dSIan Lepore "usbphy",
195c8c16b3dSIan Lepore usbphy_methods,
196c8c16b3dSIan Lepore sizeof(struct usbphy_softc)
197c8c16b3dSIan Lepore };
198c8c16b3dSIan Lepore
1997164f27dSIan Lepore /*
2007164f27dSIan Lepore * This driver needs to start before the ehci driver, but later than the usual
201458915bfSKyle Evans * "special" drivers like clocks and cpu. Ehci starts at DEFAULT so SUPPORTDEV
202458915bfSKyle Evans * is where this driver fits most.
2037164f27dSIan Lepore */
204ea538dabSJohn Baldwin EARLY_DRIVER_MODULE(usbphy, simplebus, usbphy_driver, 0, 0,
205458915bfSKyle Evans BUS_PASS_SUPPORTDEV + BUS_PASS_ORDER_MIDDLE);
206