netwalker_usb.c revision 1.5 1 1.1 bsh /*
2 1.1 bsh * Copyright (c) 2010 Genetec Corporation. All rights reserved.
3 1.1 bsh * Written by Hiroyuki Bessho for Genetec Corporation.
4 1.1 bsh *
5 1.1 bsh * Redistribution and use in source and binary forms, with or without
6 1.1 bsh * modification, are permitted provided that the following conditions
7 1.1 bsh * are met:
8 1.1 bsh * 1. Redistributions of source code must retain the above copyright
9 1.1 bsh * notice, this list of conditions and the following disclaimer.
10 1.1 bsh * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 bsh * notice, this list of conditions and the following disclaimer in the
12 1.1 bsh * documentation and/or other materials provided with the distribution.
13 1.1 bsh *
14 1.1 bsh * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
15 1.1 bsh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16 1.1 bsh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 1.1 bsh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORPORATION
18 1.1 bsh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 1.1 bsh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 1.1 bsh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 1.1 bsh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 1.1 bsh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 1.1 bsh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 1.1 bsh * POSSIBILITY OF SUCH DAMAGE.
25 1.1 bsh *
26 1.1 bsh */
27 1.1 bsh #include <sys/cdefs.h>
28 1.5 hkenken __KERNEL_RCSID(0, "$NetBSD: netwalker_usb.c,v 1.5 2019/07/24 11:20:55 hkenken Exp $");
29 1.1 bsh
30 1.1 bsh #include <sys/param.h>
31 1.1 bsh #include <sys/systm.h>
32 1.1 bsh #include <sys/conf.h>
33 1.1 bsh #include <sys/kernel.h>
34 1.1 bsh #include <sys/device.h>
35 1.1 bsh #include <sys/intr.h>
36 1.1 bsh #include <sys/bus.h>
37 1.1 bsh
38 1.1 bsh #include <dev/usb/usb.h>
39 1.1 bsh #include <dev/usb/usbdi.h>
40 1.1 bsh #include <dev/usb/usbdivar.h>
41 1.1 bsh #include <dev/usb/usb_mem.h>
42 1.1 bsh
43 1.1 bsh #include <dev/usb/ehcireg.h>
44 1.1 bsh #include <dev/usb/ehcivar.h>
45 1.1 bsh
46 1.1 bsh #include <arm/imx/imx51reg.h>
47 1.1 bsh #include <arm/imx/imx51var.h>
48 1.1 bsh #include <arm/imx/imxusbreg.h>
49 1.1 bsh #include <arm/imx/imxusbvar.h>
50 1.1 bsh #include <arm/imx/imx51_iomuxreg.h>
51 1.1 bsh #include <arm/imx/imxgpiovar.h>
52 1.1 bsh #include "locators.h"
53 1.1 bsh
54 1.1 bsh struct netwalker_usbc_softc {
55 1.5 hkenken struct imxusbc_softc sc_imxusbc; /* Must be first */
56 1.1 bsh };
57 1.1 bsh
58 1.1 bsh static int imxusbc_match(device_t, cfdata_t, void *);
59 1.1 bsh static void imxusbc_attach(device_t, device_t, void *);
60 1.1 bsh static void netwalker_usb_init(struct imxehci_softc *);
61 1.1 bsh
62 1.1 bsh static void init_otg(struct imxehci_softc *);
63 1.1 bsh static void init_h1(struct imxehci_softc *);
64 1.1 bsh
65 1.1 bsh extern const struct iomux_conf iomux_usb1_config[];
66 1.1 bsh
67 1.1 bsh /* attach structures */
68 1.1 bsh CFATTACH_DECL_NEW(imxusbc_axi, sizeof(struct netwalker_usbc_softc),
69 1.1 bsh imxusbc_match, imxusbc_attach, NULL, NULL);
70 1.1 bsh
71 1.1 bsh static int
72 1.1 bsh imxusbc_match(device_t parent, cfdata_t cf, void *aux)
73 1.1 bsh {
74 1.1 bsh struct axi_attach_args *aa = aux;
75 1.1 bsh
76 1.1 bsh printf("%s\n", __func__);
77 1.1 bsh
78 1.1 bsh if (aa->aa_addr == USBOH3_BASE)
79 1.1 bsh return 1;
80 1.1 bsh return 0;
81 1.1 bsh }
82 1.1 bsh
83 1.1 bsh static void
84 1.1 bsh imxusbc_attach(device_t parent, device_t self, void *aux)
85 1.1 bsh {
86 1.5 hkenken struct imxusbc_softc *sc = device_private(self);
87 1.1 bsh struct axi_attach_args *aa = aux;
88 1.5 hkenken
89 1.5 hkenken aprint_normal("\n");
90 1.5 hkenken aprint_normal(": Universal Serial Bus Controller\n");
91 1.5 hkenken
92 1.5 hkenken if (aa->aa_size == AXICF_SIZE_DEFAULT)
93 1.5 hkenken aa->aa_size = USBOH3_SIZE;
94 1.1 bsh
95 1.1 bsh sc->sc_init_md_hook = netwalker_usb_init;
96 1.5 hkenken sc->sc_intr_establish_md_hook = NULL;
97 1.1 bsh sc->sc_setup_md_hook = NULL;
98 1.1 bsh
99 1.5 hkenken imxusbc_attach_common(parent, self, aa->aa_iot, aa->aa_addr, aa->aa_size);
100 1.1 bsh }
101 1.1 bsh
102 1.1 bsh static void
103 1.1 bsh netwalker_usb_init(struct imxehci_softc *sc)
104 1.1 bsh {
105 1.1 bsh switch (sc->sc_unit) {
106 1.1 bsh case 0: /* OTG controller */
107 1.1 bsh init_otg(sc);
108 1.1 bsh break;
109 1.1 bsh case 1: /* EHCI Host 1 */
110 1.1 bsh init_h1(sc);
111 1.1 bsh break;
112 1.1 bsh default:
113 1.4 khorben aprint_error_dev(sc->sc_hsc.sc_dev, "unit %d not supported\n",
114 1.1 bsh sc->sc_unit);
115 1.1 bsh }
116 1.1 bsh }
117 1.1 bsh
118 1.1 bsh static void
119 1.1 bsh init_otg(struct imxehci_softc *sc)
120 1.1 bsh {
121 1.1 bsh struct imxusbc_softc *usbc = sc->sc_usbc;
122 1.1 bsh uint32_t reg;
123 1.1 bsh
124 1.1 bsh sc->sc_iftype = IMXUSBC_IF_UTMI;
125 1.1 bsh
126 1.1 bsh imxehci_reset(sc);
127 1.1 bsh
128 1.1 bsh reg = bus_space_read_4(usbc->sc_iot, usbc->sc_ioh, USBOH3_PHYCTRL0);
129 1.1 bsh reg |= PHYCTRL0_OTG_OVER_CUR_DIS;
130 1.1 bsh bus_space_write_4(usbc->sc_iot, usbc->sc_ioh, USBOH3_PHYCTRL0, reg);
131 1.1 bsh
132 1.1 bsh reg = bus_space_read_4(usbc->sc_iot, usbc->sc_ioh, USBOH3_USBCTRL);
133 1.1 bsh reg &= ~(USBCTRL_OWIR|USBCTRL_OPM);
134 1.1 bsh bus_space_write_4(usbc->sc_iot, usbc->sc_ioh, USBOH3_USBCTRL, reg);
135 1.1 bsh
136 1.1 bsh reg = bus_space_read_4(usbc->sc_iot, usbc->sc_ioh, USBOH3_PHYCTRL1);
137 1.1 bsh reg = (reg & ~PHYCTRL1_PLLDIVVALUE_MASK) | PHYCTRL1_PLLDIVVALUE_24MHZ;
138 1.1 bsh bus_space_write_4(usbc->sc_iot, usbc->sc_ioh, USBOH3_PHYCTRL1, reg);
139 1.1 bsh }
140 1.1 bsh
141 1.1 bsh static void
142 1.1 bsh init_h1(struct imxehci_softc *sc)
143 1.1 bsh {
144 1.1 bsh struct imxusbc_softc *usbc = sc->sc_usbc;
145 1.1 bsh uint32_t reg;
146 1.1 bsh
147 1.1 bsh /* output HIGH to USBH1_STP */
148 1.2 bsh gpio_data_write(GPIO_NO(1, 27), 1);
149 1.1 bsh gpio_set_direction(GPIO_NO(1, 27), GPIO_DIR_OUT);
150 1.1 bsh
151 1.1 bsh iomux_mux_config(iomux_usb1_config);
152 1.1 bsh
153 1.1 bsh delay(100 * 1000);
154 1.1 bsh
155 1.1 bsh /* XXX enable USB clock */
156 1.1 bsh
157 1.1 bsh imxehci_reset(sc);
158 1.1 bsh
159 1.1 bsh /* select external clock for Host 1 */
160 1.1 bsh reg = bus_space_read_4(usbc->sc_iot, usbc->sc_ioh,
161 1.1 bsh USBOH3_USBCTRL1);
162 1.1 bsh reg |= USBCTRL1_UH1_EXT_CLK_EN;
163 1.1 bsh bus_space_write_4(usbc->sc_iot, usbc->sc_ioh,
164 1.1 bsh USBOH3_USBCTRL1, reg);
165 1.1 bsh
166 1.1 bsh
167 1.1 bsh /* select ULPI interface for Host 1 */
168 1.1 bsh sc->sc_iftype = IMXUSBC_IF_ULPI;
169 1.1 bsh
170 1.1 bsh reg = bus_space_read_4(usbc->sc_iot, usbc->sc_ioh,
171 1.1 bsh USBOH3_USBCTRL);
172 1.1 bsh reg &= ~(USBCTRL_H1PM);
173 1.1 bsh reg |= USBCTRL_H1UIE|USBCTRL_H1WIE;
174 1.1 bsh bus_space_write_4(usbc->sc_iot, usbc->sc_ioh,
175 1.1 bsh USBOH3_USBCTRL, reg);
176 1.1 bsh
177 1.2 bsh iomux_set_function(MUX_PIN(USBH1_STP), IOMUX_CONFIG_ALT0);
178 1.1 bsh
179 1.1 bsh
180 1.1 bsh /* HUB RESET release */
181 1.1 bsh gpio_data_write(GPIO_NO(1, 7), 1);
182 1.1 bsh gpio_set_direction(GPIO_NO(1, 7), GPIO_DIR_OUT);
183 1.1 bsh
184 1.1 bsh /* Drive 26M_OSC_EN line high 3_1 */
185 1.1 bsh gpio_data_write(GPIO_NO(3, 1), 1);
186 1.1 bsh gpio_set_direction(GPIO_NO(3, 1), GPIO_DIR_OUT);
187 1.1 bsh
188 1.1 bsh /* Drive USB_CLK_EN_B line low 2_1 */
189 1.1 bsh gpio_data_write(GPIO_NO(2, 1), 0);
190 1.1 bsh gpio_set_direction(GPIO_NO(2, 1), GPIO_DIR_IN);
191 1.1 bsh
192 1.1 bsh /* MX51_PIN_EIM_D21 - De-assert USB PHY RESETB */
193 1.1 bsh delay(10 * 1000);
194 1.1 bsh gpio_data_write(GPIO_NO(2, 5), 1);
195 1.1 bsh gpio_set_direction(GPIO_NO(2, 5), GPIO_DIR_OUT);
196 1.2 bsh iomux_set_function(MUX_PIN(EIM_D21), IOMUX_CONFIG_ALT1);
197 1.1 bsh delay(5 * 1000);
198 1.1 bsh }
199 1.1 bsh
200 1.1 bsh /*
201 1.1 bsh * IOMUX setting for USB Host1
202 1.1 bsh * taken from Linux driver
203 1.1 bsh */
204 1.1 bsh const struct iomux_conf iomux_usb1_config[] = {
205 1.1 bsh
206 1.1 bsh {
207 1.1 bsh /* Initially setup this pin for GPIO, and change to
208 1.1 bsh * USBH1_STP later */
209 1.2 bsh .pin = MUX_PIN(USBH1_STP),
210 1.1 bsh .mux = IOMUX_CONFIG_ALT2,
211 1.1 bsh .pad = (PAD_CTL_SRE | PAD_CTL_DSE_HIGH |
212 1.1 bsh PAD_CTL_KEEPER | PAD_CTL_HYS)
213 1.1 bsh },
214 1.1 bsh
215 1.1 bsh {
216 1.1 bsh /* Clock */
217 1.2 bsh .pin = MUX_PIN(USBH1_CLK),
218 1.1 bsh .mux = IOMUX_CONFIG_ALT0,
219 1.2 bsh .pad = (PAD_CTL_SRE | PAD_CTL_DSE_HIGH |
220 1.1 bsh PAD_CTL_KEEPER | PAD_CTL_HYS)
221 1.1 bsh },
222 1.1 bsh {
223 1.1 bsh /* DIR */
224 1.2 bsh .pin = MUX_PIN(USBH1_DIR),
225 1.1 bsh .mux = IOMUX_CONFIG_ALT0,
226 1.1 bsh .pad = (PAD_CTL_SRE | PAD_CTL_DSE_HIGH |
227 1.1 bsh PAD_CTL_KEEPER | PAD_CTL_HYS)
228 1.1 bsh },
229 1.1 bsh
230 1.1 bsh {
231 1.1 bsh /* NXT */
232 1.2 bsh .pin = MUX_PIN(USBH1_NXT),
233 1.1 bsh .mux = IOMUX_CONFIG_ALT0,
234 1.1 bsh .pad = (PAD_CTL_SRE | PAD_CTL_DSE_HIGH |
235 1.1 bsh PAD_CTL_KEEPER | PAD_CTL_HYS)
236 1.1 bsh },
237 1.1 bsh
238 1.1 bsh #define USBH1_DATA_CONFIG(n) \
239 1.1 bsh { \
240 1.1 bsh /* DATA n */ \
241 1.2 bsh .pin = MUX_PIN(USBH1_DATA##n), \
242 1.1 bsh .mux = IOMUX_CONFIG_ALT0, \
243 1.1 bsh .pad = (PAD_CTL_SRE | PAD_CTL_DSE_HIGH | \
244 1.1 bsh PAD_CTL_KEEPER | PAD_CTL_PUS_100K_PU | \
245 1.1 bsh PAD_CTL_HYS), \
246 1.1 bsh /* XXX: what does 100K_PU with KEEPER ? */ \
247 1.1 bsh }
248 1.1 bsh
249 1.1 bsh USBH1_DATA_CONFIG(0),
250 1.1 bsh USBH1_DATA_CONFIG(1),
251 1.1 bsh USBH1_DATA_CONFIG(2),
252 1.1 bsh USBH1_DATA_CONFIG(3),
253 1.1 bsh USBH1_DATA_CONFIG(4),
254 1.1 bsh USBH1_DATA_CONFIG(5),
255 1.1 bsh USBH1_DATA_CONFIG(6),
256 1.1 bsh USBH1_DATA_CONFIG(7),
257 1.1 bsh
258 1.1 bsh {
259 1.1 bsh /* USB_CLK_EN_B GPIO2[1]*/
260 1.2 bsh .pin = MUX_PIN(EIM_D17),
261 1.1 bsh .mux = IOMUX_CONFIG_ALT1,
262 1.1 bsh .pad = (PAD_CTL_DSE_HIGH | PAD_CTL_PKE | PAD_CTL_SRE),
263 1.1 bsh },
264 1.1 bsh
265 1.1 bsh {
266 1.1 bsh /* USB PHY RESETB */
267 1.2 bsh .pin = MUX_PIN(EIM_D21),
268 1.1 bsh .mux = IOMUX_CONFIG_ALT1,
269 1.1 bsh .pad = (PAD_CTL_DSE_HIGH | PAD_CTL_KEEPER |
270 1.1 bsh PAD_CTL_PUS_100K_PU | PAD_CTL_SRE)
271 1.1 bsh },
272 1.1 bsh {
273 1.1 bsh /* USB HUB RESET */
274 1.2 bsh .pin = MUX_PIN(GPIO1_7),
275 1.1 bsh .mux = IOMUX_CONFIG_ALT0,
276 1.1 bsh .pad = (PAD_CTL_DSE_HIGH | PAD_CTL_SRE),
277 1.1 bsh },
278 1.3 bsh {
279 1.3 bsh /* 26M_OSC pin settings */
280 1.3 bsh .pin = MUX_PIN(DI1_PIN12),
281 1.3 bsh .mux = IOMUX_CONFIG_ALT4,
282 1.3 bsh .pad = (PAD_CTL_DSE_HIGH | PAD_CTL_KEEPER |
283 1.3 bsh PAD_CTL_SRE),
284 1.3 bsh },
285 1.1 bsh
286 1.1 bsh /* end of table */
287 1.1 bsh {.pin = IOMUX_CONF_EOT}
288 1.1 bsh };
289