1/*	$NetBSD: imx6_usb.c,v 1.8 2023/05/04 17:09:45 bouyer 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(0, "$NetBSD: imx6_usb.c,v 1.8 2023/05/04 17:09:45 bouyer Exp $");
31
32#include "opt_fdt.h"
33
34#include <locators.h>
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/conf.h>
39#include <sys/kernel.h>
40#include <sys/device.h>
41#include <sys/intr.h>
42#include <sys/bus.h>
43
44#include <dev/usb/usb.h>
45#include <dev/usb/usbdi.h>
46#include <dev/usb/usbdivar.h>
47#include <dev/usb/usb_mem.h>
48
49#include <dev/usb/ehcireg.h>
50#include <dev/usb/ehcivar.h>
51
52#include <arm/nxp/imx6_usbreg.h>
53#include <arm/imx/imxusbvar.h>
54
55#include <dev/fdt/fdtvar.h>
56
57struct imxusbc_fdt_softc {
58	struct imxusbc_softc sc_imxusbc; /* Must be first */
59
60	int sc_phandle;
61};
62
63static int imx6_usb_match(device_t, struct cfdata *, void *);
64static void imx6_usb_attach(device_t, device_t, void *);
65static int imx6_usb_init_clocks(struct imxusbc_softc *);
66static void imx6_usb_init(struct imxehci_softc *, uintptr_t);
67static void init_otg(struct imxehci_softc *);
68static void init_h1(struct imxehci_softc *);
69static int imxusbc_print(void *, const char *);
70static void *imx6_usb_intr_establish(struct imxehci_softc *, uintptr_t);
71
72/* attach structures */
73CFATTACH_DECL_NEW(imxusbc_fdt, sizeof(struct imxusbc_fdt_softc),
74    imx6_usb_match, imx6_usb_attach, NULL, NULL);
75
76static const struct device_compatible_entry compat_data[] = {
77	{ .compat = "fsl,imx6q-usb", .value = 1 },
78	{ .compat = "fsl,imx6sx-usb", .value = 2 },
79	{ .compat = "fsl,imx7d-usb", .value = 1 },
80	DEVICE_COMPAT_EOL
81};
82
83static int
84imx6_usb_match(device_t parent, cfdata_t cf, void *aux)
85{
86	struct fdt_attach_args * const faa = aux;
87
88	return of_compatible_match(faa->faa_phandle, compat_data);
89}
90
91static void
92imx6_usb_attach(device_t parent, device_t self, void *aux)
93{
94	struct imxusbc_fdt_softc *ifsc = device_private(self);
95	struct imxusbc_softc *sc = &ifsc->sc_imxusbc;
96	struct fdt_attach_args * const faa = aux;
97	const int phandle = faa->faa_phandle;
98	bus_space_tag_t bst = faa->faa_bst;
99	bus_space_handle_t bsh;
100	bus_addr_t addr;
101	bus_size_t size;
102	int error;
103	struct fdtbus_regulator *reg;
104
105	aprint_naive("\n");
106	aprint_normal("\n");
107
108	ifsc->sc_phandle = phandle;
109
110	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
111		aprint_error(": couldn't get imxusbc registers\n");
112		return;
113	}
114
115	error = bus_space_map(bst, addr, size, 0, &bsh);
116	if (error) {
117		aprint_error(": couldn't map %#" PRIxBUSADDR ": %d", addr, error);
118		return;
119	}
120
121	sc->sc_clk = fdtbus_clock_get_index(phandle, 0);
122	if (sc->sc_clk == NULL) {
123		aprint_error(": couldn't get clock\n");
124		return;
125	}
126
127	sc->sc_init_md_hook = imx6_usb_init;
128	sc->sc_intr_establish_md_hook = imx6_usb_intr_establish;
129	sc->sc_setup_md_hook = NULL;
130	sc->sc_md_hook_data = of_compatible_lookup(phandle, compat_data)->value;
131
132	sc->sc_dev = self;
133	sc->sc_iot = bst;
134	sc->sc_ioh = bsh;
135	sc->sc_ehci_size = size;
136	sc->sc_ehci_offset = 0;
137
138	struct fdt_phandle_data data;
139	error = fdtbus_get_phandle_with_data(phandle, "fsl,usbmisc",
140	    "#index-cells", 0, &data);
141	if (error) {
142		aprint_error(": couldn't get usbmisc property\n");
143		return;
144	}
145	int unit = be32toh(data.values[0]);
146
147	if (fdtbus_get_reg(data.phandle, 0, &addr, &size) != 0) {
148		aprint_error(": couldn't get usbmisc registers\n");
149		return;
150	}
151	error = bus_space_map(bst, addr, size, 0, &bsh);
152	if (error) {
153		aprint_error(": couldn't map usbmisc registers: %d\n", error);
154		return;
155	}
156	sc->sc_ioh_usbnc = bsh;
157
158	if (imx6_usb_init_clocks(sc) != 0) {
159		aprint_error_dev(self, "couldn't init clocks\n");
160		return;
161	}
162
163	/* attach OTG/EHCI host controllers */
164	struct imxusbc_attach_args iaa;
165	iaa.aa_iot = sc->sc_iot;
166	iaa.aa_ioh = sc->sc_ioh;
167	iaa.aa_dmat = faa->faa_dmat;
168	iaa.aa_unit = unit;
169	iaa.aa_irq = IMXUSBCCF_IRQ_DEFAULT;
170	config_found(self, &iaa, imxusbc_print,
171	    CFARGS_NONE);
172
173	if (of_hasprop(phandle, "vbus-supply")) {
174		reg = fdtbus_regulator_acquire(phandle, "vbus-supply");
175		if (reg == NULL) {
176			aprint_error_dev(self,
177			    "couldn't acquire vbus-supply\n");
178		} else {
179			error = fdtbus_regulator_enable(reg);
180			if (error != 0) {
181				aprint_error_dev(self,
182				    "couldn't enable vbus-supply\n");
183			}
184		}
185	} else {
186		aprint_verbose_dev(self, "no regulator\n");
187	}
188	return;
189}
190
191static int
192imxusbc_print(void *aux, const char *name __unused)
193{
194	struct imxusbc_attach_args *iaa;
195
196	iaa = (struct imxusbc_attach_args *)aux;
197
198	aprint_normal(" unit %d", iaa->aa_unit);
199	return UNCONF;
200}
201
202
203static int
204imx6_usb_init_clocks(struct imxusbc_softc *sc)
205{
206	int error;
207
208	error = clk_enable(sc->sc_clk);
209	if (error) {
210		aprint_error_dev(sc->sc_dev, "couldn't enable: %d\n", error);
211		return error;
212	}
213
214	return 0;
215}
216
217static void
218imx6_usb_init(struct imxehci_softc *sc, uintptr_t data)
219{
220	int notg = data;
221
222	switch (sc->sc_unit) {
223	case 0:	/* OTG controller */
224		init_otg(sc);
225		break;
226	case 1:	/* EHCI Host 1 */
227		if (notg >= 2)
228			init_otg(sc);
229		else
230			init_h1(sc);
231		break;
232	case 2:	/* EHCI Host 2 */
233	case 3:	/* EHCI Host 3 */
234	default:
235		aprint_error_dev(sc->sc_dev, "unit %d not supported\n",
236		    sc->sc_unit);
237	}
238}
239
240static void
241init_otg(struct imxehci_softc *sc)
242{
243	struct imxusbc_softc *usbc = sc->sc_usbc;
244	uint32_t v;
245
246	sc->sc_iftype = IMXUSBC_IF_UTMI_WIDE;
247
248	imxehci_reset(sc);
249
250	v = bus_space_read_4(usbc->sc_iot, usbc->sc_ioh_usbnc, USBNC_USB_OTG_CTRL);
251	v |= USBNC_USB_OTG_CTRL_WKUP_VBUS_EN;
252	v |= USBNC_USB_OTG_CTRL_OVER_CUR_DIS;
253	v |= USBNC_USB_OTG_CTRL_PWR_POL;
254	v &= ~USBNC_USB_OTG_CTRL_UTMI_ON_CLOCK;
255	bus_space_write_4(usbc->sc_iot, usbc->sc_ioh_usbnc, USBNC_USB_OTG_CTRL, v);
256}
257
258static void
259init_h1(struct imxehci_softc *sc)
260{
261	struct imxusbc_softc *usbc = sc->sc_usbc;
262	uint32_t v;
263
264	sc->sc_iftype = IMXUSBC_IF_UTMI_WIDE;
265
266	v = bus_space_read_4(usbc->sc_iot, usbc->sc_ioh_usbnc, USBNC_USB_UH1_CTRL);
267	v |= USBNC_USB_UH1_CTRL_OVER_CUR_POL;
268	v |= USBNC_USB_UH1_CTRL_OVER_CUR_DIS;
269	bus_space_write_4(usbc->sc_iot, usbc->sc_ioh_usbnc, USBNC_USB_UH1_CTRL, v);
270
271	/* do reset */
272	imxehci_reset(sc);
273
274	/* set mode */
275	v = bus_space_read_4(usbc->sc_iot, usbc->sc_ioh, USBC_UH1_USBMODE);
276	v &= ~USBC_UH_USBMODE_CM;
277	v |= __SHIFTIN(USBC_UH_USBMODE_CM, USBC_UH_USBMODE_CM_HOST_CONTROLLER);
278	bus_space_write_4(usbc->sc_iot, usbc->sc_ioh, USBC_UH1_USBMODE, v);
279}
280
281static void *
282imx6_usb_intr_establish(struct imxehci_softc *sc, uintptr_t data)
283{
284	struct imxusbc_fdt_softc *ifsc = (struct imxusbc_fdt_softc *)sc->sc_usbc;
285	ehci_softc_t *hsc = &sc->sc_hsc;
286	void *ih;
287
288	char intrstr[128];
289	if (!fdtbus_intr_str(ifsc->sc_phandle, 0, intrstr, sizeof(intrstr))) {
290		aprint_error_dev(sc->sc_dev, "failed to decode interrupt\n");
291		return NULL;
292	}
293	ih = fdtbus_intr_establish_xname(ifsc->sc_phandle, 0, IPL_USB,
294	    FDT_INTR_MPSAFE, ehci_intr, hsc, device_xname(sc->sc_dev));
295	if (ih == NULL) {
296		aprint_error_dev(sc->sc_dev, "failed to establish interrupt on %s\n",
297		    intrstr);
298		return NULL;
299	}
300	aprint_normal_dev(sc->sc_dev, "interrupting on %s\n", intrstr);
301
302	return ih;
303}
304