xref: /netbsd-src/sys/arch/arm/nxp/imx6_usbphy.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1 /*	$NetBSD: imx6_usbphy.c,v 1.2 2021/01/27 03:10:20 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2019 Genetec Corporation.  All rights reserved.
5  * Written by Hashimoto Kenichi for Genetec Corporation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(1, "$NetBSD: imx6_usbphy.c,v 1.2 2021/01/27 03:10:20 thorpej Exp $");
31 
32 #include "opt_fdt.h"
33 
34 #include "locators.h"
35 #include "ohci.h"
36 #include "ehci.h"
37 
38 #include <sys/param.h>
39 #include <sys/bus.h>
40 #include <sys/device.h>
41 
42 #include <arm/nxp/imx6_usbphyreg.h>
43 
44 #include <dev/fdt/fdtvar.h>
45 
46 struct imx6_usbphy_softc {
47 	device_t sc_dev;
48 
49 	bus_space_tag_t sc_iot;
50 	bus_space_handle_t sc_ioh;
51 
52 	struct clk *sc_clk;
53 };
54 
55 static int imx6_usbphy_match(device_t, cfdata_t, void *);
56 static void imx6_usbphy_attach(device_t, device_t, void *);
57 
58 static int imx6_usbphy_init_clocks(device_t);
59 static int imx6_usbphy_enable(device_t, void *, bool);
60 
61 CFATTACH_DECL_NEW(imxusbphy, sizeof(struct imx6_usbphy_softc),
62     imx6_usbphy_match, imx6_usbphy_attach, NULL, NULL);
63 
64 static const struct device_compatible_entry compat_data[] = {
65 	{ .compat = "fsl,imx6q-usbphy" },
66 	DEVICE_COMPAT_EOL
67 };
68 
69 static int
70 imx6_usbphy_match(device_t parent, cfdata_t cf, void *aux)
71 {
72 	struct fdt_attach_args * const faa = aux;
73 
74 	return of_compatible_match(faa->faa_phandle, compat_data);
75 }
76 
77 static void
78 imx6_usbphy_attach(device_t parent, device_t self, void *aux)
79 {
80 	struct imx6_usbphy_softc *sc = device_private(self);
81 	struct fdt_attach_args * const faa = aux;
82 	const int phandle = faa->faa_phandle;
83 	bus_space_tag_t bst = faa->faa_bst;
84 	bus_space_handle_t bsh;
85 	bus_addr_t addr;
86 	bus_size_t size;
87 	int error;
88 
89 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
90 		aprint_error(": couldn't get iomux registers\n");
91 		return;
92 	}
93 
94 	error = bus_space_map(bst, addr, size, 0, &bsh);
95 	if (error) {
96 		aprint_error(": couldn't map %#" PRIxBUSADDR ": %d", addr, error);
97 		return;
98 	}
99 
100 	sc->sc_clk = fdtbus_clock_get_index(phandle, 0);
101 	if (sc->sc_clk == NULL) {
102 		aprint_error(": couldn't get clock\n");
103 		return;
104 	}
105 
106 	sc->sc_dev = self;
107 	sc->sc_iot = bst;
108 	sc->sc_ioh = bsh;
109 
110 	aprint_naive("\n");
111 	aprint_normal(": USB PHY\n");
112 
113 	imx6_usbphy_init_clocks(self);
114 	imx6_usbphy_enable(self, NULL, true);
115 }
116 
117 static int
118 imx6_usbphy_init_clocks(device_t dev)
119 {
120 	struct imx6_usbphy_softc * const sc = device_private(dev);
121 	int error;
122 
123 	error = clk_enable(sc->sc_clk);
124 	if (error) {
125 		aprint_error_dev(sc->sc_dev, "couldn't enable: %d\n", error);
126 		return error;
127 	}
128 
129 	return 0;
130 }
131 
132 #define	USBPHY_READ(sc, reg)						      \
133 	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
134 #define	USBPHY_WRITE(sc, reg, val)					      \
135 	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
136 
137 static int
138 imx6_usbphy_enable(device_t dev, void *priv, bool enable)
139 {
140 	struct imx6_usbphy_softc * const sc = device_private(dev);
141 
142 	/* USBPHY enable */
143 	USBPHY_WRITE(sc, USBPHY_CTRL, USBPHY_CTRL_CLKGATE);
144 
145 	/* do reset */
146 	USBPHY_WRITE(sc, USBPHY_CTRL_SET, USBPHY_CTRL_SFTRST);
147 	delay(100);
148 
149 	/* clear reset, and run clocks */
150 	USBPHY_WRITE(sc, USBPHY_CTRL_CLR,
151 	    USBPHY_CTRL_SFTRST | USBPHY_CTRL_CLKGATE);
152 	delay(100);
153 
154 	/* power on */
155 	USBPHY_WRITE(sc, USBPHY_PWD, 0);
156 
157 	/* UTMI+Level2, Level3 */
158 	USBPHY_WRITE(sc, USBPHY_CTRL_SET,
159 	    USBPHY_CTRL_ENUTMILEVEL2 | USBPHY_CTRL_ENUTMILEVEL3);
160 
161 	return 0;
162 }
163 
164