xref: /netbsd-src/sys/arch/arm/imx/imx23_usb.c (revision 0b303a9fd61d02c3ad67a62b4890948945ac163c)
1 /* $Id: imx23_usb.c,v 1.7 2024/01/15 17:29:27 andvar Exp $ */
2 
3 /*
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Petri Laakso.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/bus.h>
35 #include <sys/errno.h>
36 
37 #include <arm/pic/picvar.h>
38 
39 #include <dev/usb/usb.h>
40 #include <dev/usb/usbdi.h>
41 #include <dev/usb/usbdivar.h>
42 #include <dev/usb/usb_mem.h>
43 #include <dev/usb/ehcireg.h>
44 #include <dev/usb/ehcivar.h>
45 #include <arm/imx/imxusbvar.h>
46 #include <arm/imx/imxusbreg.h>
47 
48 #include <arm/mainbus/mainbus.h>
49 
50 #include <arm/imx/imx23_clkctrlvar.h>
51 #include <arm/imx/imx23_digctlvar.h>
52 #include <arm/imx/imx23_pinctrlvar.h>
53 #include <arm/imx/imx23var.h>
54 
55 #include "locators.h"
56 
57 struct imx23_usb_softc {
58 	struct imxusbc_softc  sc_imxusbc; /* Must be first */
59 };
60 
61 static int	imx23_usb_match(device_t, cfdata_t, void *);
62 static void	imx23_usb_attach(device_t, device_t, void *);
63 static int	imx23_usb_activate(device_t, enum devact);
64 
65 static int      imxusbc_search(device_t, cfdata_t, const int *, void *);
66 static void	imx23_usb_init(struct imxehci_softc *, uintptr_t);
67 
68 CFATTACH_DECL3_NEW(imxusbc,
69         sizeof(struct imx23_usb_softc),
70         imx23_usb_match,
71         imx23_usb_attach,
72         NULL,
73         imx23_usb_activate,
74         NULL,
75         NULL,
76         0
77 );
78 
79 #define AHB_USB		0x80080000
80 #define AHB_USB_SIZE	0x40000
81 
82 static int
imx23_usb_match(device_t parent,cfdata_t match,void * aux)83 imx23_usb_match(device_t parent, cfdata_t match, void *aux)
84 {
85 	struct ahb_attach_args *aa = aux;
86 
87 	if ((aa->aa_addr == AHB_USB) && (aa->aa_size == AHB_USB_SIZE))
88 		return 1;
89 
90 	return 0;
91 }
92 
93 static void
imx23_usb_attach(device_t parent,device_t self,void * aux)94 imx23_usb_attach(device_t parent, device_t self, void *aux)
95 {
96 	struct imxusbc_softc *sc = device_private(self);
97 
98 	sc->sc_dev = self;
99 	sc->sc_iot = &imx23_bus_space;
100 	sc->sc_ehci_size = IMXUSB_EHCI_SIZE;
101 	sc->sc_ehci_offset = IMXUSB_EHCI_SIZE;
102 
103 	sc->sc_init_md_hook = imx23_usb_init;
104 	sc->sc_intr_establish_md_hook = NULL;
105 	sc->sc_setup_md_hook = NULL;
106 
107 	if (bus_space_map(sc->sc_iot, AHB_USB, AHB_USB_SIZE, 0, &sc->sc_ioh)) {
108 		aprint_error_dev(sc->sc_dev, "Unable to map bus space");
109 		return;
110 	}
111 
112 	/* Enable PLL outputs for USB PHY. */
113 	clkctrl_en_usb();
114 
115 	/* Enable external USB chip. */
116 	imx23_pinctrl_en_usb();
117 
118 	/* USB clock on. */
119 	digctl_usb_clkgate(0);
120 
121 	aprint_normal("\n");
122 
123 	/* attach OTG/EHCI host controllers */
124 	config_search(self, NULL,
125 	    CFARGS(.search = imxusbc_search));
126 
127 	return;
128 }
129 
130 static int
imx23_usb_activate(device_t self,enum devact act)131 imx23_usb_activate(device_t self, enum devact act)
132 {
133 	return EOPNOTSUPP;
134 }
135 
136 static int
imxusbc_search(device_t parent,cfdata_t cf,const int * ldesc,void * aux)137 imxusbc_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
138 {
139         struct imxusbc_softc *sc = device_private(parent);
140         struct imxusbc_attach_args aa;
141 
142         aa.aa_iot = sc->sc_iot;
143         aa.aa_ioh = sc->sc_ioh;
144         aa.aa_dmat = &imx23_bus_dma_tag;
145         aa.aa_unit = cf->cf_loc[IMXUSBCCF_UNIT];
146         aa.aa_irq = cf->cf_loc[IMXUSBCCF_IRQ];
147 
148         if (config_probe(parent, cf, &aa))
149                 config_attach(parent, cf, &aa, NULL, CFARGS_NONE);
150 
151         return 0;
152 }
153 
154 static
imx23_usb_init(struct imxehci_softc * sc,uintptr_t data)155 void imx23_usb_init(struct imxehci_softc *sc, uintptr_t data)
156 {
157 
158 	sc->sc_iftype = IMXUSBC_IF_UTMI;
159 
160 	return;
161 }
162