1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2010-2012 Semihalf
5 * All rights reserved.
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 AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, 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 #include "opt_bus.h"
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/queue.h>
38 #include <sys/lock.h>
39 #include <sys/lockmgr.h>
40 #include <sys/condvar.h>
41 #include <sys/rman.h>
42
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45
46 #include <dev/usb/usb.h>
47 #include <dev/usb/usbdi.h>
48 #include <dev/usb/usb_core.h>
49 #include <dev/usb/usb_busdma.h>
50 #include <dev/usb/usb_process.h>
51 #include <dev/usb/usb_util.h>
52 #include <dev/usb/usb_controller.h>
53 #include <dev/usb/usb_bus.h>
54 #include <dev/usb/controller/ehci.h>
55 #include <dev/usb/controller/ehcireg.h>
56
57 #include <machine/bus.h>
58 #include <machine/clock.h>
59 #include <machine/resource.h>
60
61 #include <powerpc/include/tlb.h>
62
63 #include "opt_platform.h"
64
65 /*
66 * Register the driver
67 */
68 /* Forward declarations */
69 static int fsl_ehci_attach(device_t self);
70 static int fsl_ehci_detach(device_t self);
71 static int fsl_ehci_probe(device_t self);
72
73 static device_method_t ehci_methods[] = {
74 /* Device interface */
75 DEVMETHOD(device_probe, fsl_ehci_probe),
76 DEVMETHOD(device_attach, fsl_ehci_attach),
77 DEVMETHOD(device_detach, fsl_ehci_detach),
78 DEVMETHOD(device_suspend, bus_generic_suspend),
79 DEVMETHOD(device_resume, bus_generic_resume),
80 DEVMETHOD(device_shutdown, bus_generic_shutdown),
81
82 /* Bus interface */
83 DEVMETHOD(bus_print_child, bus_generic_print_child),
84 { 0, 0 }
85 };
86
87 /* kobj_class definition */
88 static driver_t ehci_driver = {
89 "ehci",
90 ehci_methods,
91 sizeof(struct ehci_softc)
92 };
93
94 DRIVER_MODULE(ehci, simplebus, ehci_driver, 0, 0);
95 MODULE_DEPEND(ehci, usb, 1, 1, 1);
96
97 /*
98 * Private defines
99 */
100 #define FSL_EHCI_REG_OFF 0x100
101 #define FSL_EHCI_REG_SIZE 0x300
102
103 /*
104 * Internal interface registers' offsets.
105 * Offsets from 0x000 ehci dev space, big-endian access.
106 */
107 enum internal_reg {
108 SNOOP1 = 0x400,
109 SNOOP2 = 0x404,
110 AGE_CNT_THRESH = 0x408,
111 SI_CTRL = 0x410,
112 CONTROL = 0x500
113 };
114
115 /* CONTROL register bit flags */
116 enum control_flags {
117 USB_EN = 0x00000004,
118 UTMI_PHY_EN = 0x00000200,
119 ULPI_INT_EN = 0x00000001
120 };
121
122 /* SI_CTRL register bit flags */
123 enum si_ctrl_flags {
124 FETCH_32 = 1,
125 FETCH_64 = 0
126 };
127
128 #define SNOOP_RANGE_2GB 0x1E
129
130 /*
131 * Operational registers' offsets.
132 * Offsets from USBCMD register, little-endian access.
133 */
134 enum special_op_reg {
135 USBMODE = 0x0A8,
136 PORTSC = 0x084,
137 ULPI_VIEWPORT = 0x70
138 };
139
140 /* USBMODE register bit flags */
141 enum usbmode_flags {
142 HOST_MODE = 0x3,
143 DEVICE_MODE = 0x2
144 };
145
146 #define PORT_POWER_MASK 0x00001000
147
148 /*
149 * Private methods
150 */
151
152 static void
set_to_host_mode(ehci_softc_t * sc)153 set_to_host_mode(ehci_softc_t *sc)
154 {
155 int tmp;
156
157 tmp = bus_space_read_4(sc->sc_io_tag, sc->sc_io_hdl, USBMODE);
158 bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl, USBMODE, tmp | HOST_MODE);
159 }
160
161 static void
enable_usb(device_t dev,bus_space_tag_t iot,bus_space_handle_t ioh)162 enable_usb(device_t dev, bus_space_tag_t iot, bus_space_handle_t ioh)
163 {
164 int tmp;
165 phandle_t node;
166 char *phy_type;
167
168 phy_type = NULL;
169 tmp = bus_space_read_4(iot, ioh, CONTROL) | USB_EN;
170
171 node = ofw_bus_get_node(dev);
172 if ((node != 0) &&
173 (OF_getprop_alloc(node, "phy_type", (void **)&phy_type) > 0)) {
174 if (strncasecmp(phy_type, "utmi", strlen("utmi")) == 0)
175 tmp |= UTMI_PHY_EN;
176 OF_prop_free(phy_type);
177 }
178 bus_space_write_4(iot, ioh, CONTROL, tmp);
179 }
180
181 static void
set_32b_prefetch(bus_space_tag_t iot,bus_space_handle_t ioh)182 set_32b_prefetch(bus_space_tag_t iot, bus_space_handle_t ioh)
183 {
184
185 bus_space_write_4(iot, ioh, SI_CTRL, FETCH_32);
186 }
187
188 static void
set_snooping(bus_space_tag_t iot,bus_space_handle_t ioh)189 set_snooping(bus_space_tag_t iot, bus_space_handle_t ioh)
190 {
191
192 bus_space_write_4(iot, ioh, SNOOP1, SNOOP_RANGE_2GB);
193 bus_space_write_4(iot, ioh, SNOOP2, 0x80000000 | SNOOP_RANGE_2GB);
194 }
195
196 static void
clear_port_power(ehci_softc_t * sc)197 clear_port_power(ehci_softc_t *sc)
198 {
199 int tmp;
200
201 tmp = bus_space_read_4(sc->sc_io_tag, sc->sc_io_hdl, PORTSC);
202 bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl, PORTSC, tmp & ~PORT_POWER_MASK);
203 }
204
205 /*
206 * Public methods
207 */
208 static int
fsl_ehci_probe(device_t dev)209 fsl_ehci_probe(device_t dev)
210 {
211
212 if (!ofw_bus_status_okay(dev))
213 return (ENXIO);
214
215 if (((ofw_bus_is_compatible(dev, "fsl-usb2-dr")) == 0) &&
216 ((ofw_bus_is_compatible(dev, "fsl-usb2-mph")) == 0))
217 return (ENXIO);
218
219 device_set_desc(dev, "Freescale integrated EHCI controller");
220
221 return (BUS_PROBE_DEFAULT);
222 }
223
224 static int
fsl_ehci_attach(device_t self)225 fsl_ehci_attach(device_t self)
226 {
227 ehci_softc_t *sc;
228 int rid;
229 int err;
230 bus_space_handle_t ioh;
231 bus_space_tag_t iot;
232
233 sc = device_get_softc(self);
234 rid = 0;
235
236 sc->sc_bus.parent = self;
237 sc->sc_bus.devices = sc->sc_devices;
238 sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
239 sc->sc_bus.dma_bits = 32;
240
241 if (usb_bus_mem_alloc_all(&sc->sc_bus,
242 USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc))
243 return (ENOMEM);
244
245 /* Allocate io resource for EHCI */
246 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
247 RF_ACTIVE);
248 if (sc->sc_io_res == NULL) {
249 err = fsl_ehci_detach(self);
250 if (err) {
251 device_printf(self,
252 "Detach of the driver failed with error %d\n",
253 err);
254 }
255 return (ENXIO);
256 }
257 iot = rman_get_bustag(sc->sc_io_res);
258
259 /*
260 * Set handle to USB related registers subregion used by generic
261 * EHCI driver
262 */
263 ioh = rman_get_bushandle(sc->sc_io_res);
264
265 err = bus_space_subregion(iot, ioh, FSL_EHCI_REG_OFF, FSL_EHCI_REG_SIZE,
266 &sc->sc_io_hdl);
267 if (err != 0) {
268 err = fsl_ehci_detach(self);
269 if (err) {
270 device_printf(self,
271 "Detach of the driver failed with error %d\n",
272 err);
273 }
274 return (ENXIO);
275 }
276
277 /* Set little-endian tag for use by the generic EHCI driver */
278 sc->sc_io_tag = &bs_le_tag;
279
280 /* Allocate irq */
281 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
282 RF_ACTIVE);
283 if (sc->sc_irq_res == NULL) {
284 err = fsl_ehci_detach(self);
285 if (err) {
286 device_printf(self,
287 "Detach of the driver failed with error %d\n",
288 err);
289 }
290 return (ENXIO);
291 }
292
293 /* Setup interrupt handler */
294 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
295 NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
296 if (err) {
297 device_printf(self, "Could not setup irq, %d\n", err);
298 sc->sc_intr_hdl = NULL;
299 err = fsl_ehci_detach(self);
300 if (err) {
301 device_printf(self,
302 "Detach of the driver failed with error %d\n",
303 err);
304 }
305 return (ENXIO);
306 }
307
308 /* Add USB device */
309 sc->sc_bus.bdev = device_add_child(self, "usbus", DEVICE_UNIT_ANY);
310 if (!sc->sc_bus.bdev) {
311 device_printf(self, "Could not add USB device\n");
312 err = fsl_ehci_detach(self);
313 if (err) {
314 device_printf(self,
315 "Detach of the driver failed with error %d\n",
316 err);
317 }
318 return (ENOMEM);
319 }
320 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
321
322 sc->sc_id_vendor = 0x1234;
323 strlcpy(sc->sc_vendor, "Freescale", sizeof(sc->sc_vendor));
324
325 /* Enable USB */
326 err = ehci_reset(sc);
327 if (err) {
328 device_printf(self, "Could not reset the controller\n");
329 err = fsl_ehci_detach(self);
330 if (err) {
331 device_printf(self,
332 "Detach of the driver failed with error %d\n",
333 err);
334 }
335 return (ENXIO);
336 }
337
338 enable_usb(self, iot, ioh);
339 set_snooping(iot, ioh);
340 set_to_host_mode(sc);
341 set_32b_prefetch(iot, ioh);
342
343 /*
344 * If usb subsystem is enabled in U-Boot, port power has to be turned
345 * off to allow proper discovery of devices during boot up.
346 */
347 clear_port_power(sc);
348
349 /* Set flags */
350 sc->sc_flags |= EHCI_SCFLG_DONTRESET | EHCI_SCFLG_NORESTERM;
351
352 err = ehci_init(sc);
353 if (!err) {
354 sc->sc_flags |= EHCI_SCFLG_DONEINIT;
355 err = device_probe_and_attach(sc->sc_bus.bdev);
356 }
357
358 if (err) {
359 device_printf(self, "USB init failed err=%d\n", err);
360 err = fsl_ehci_detach(self);
361 if (err) {
362 device_printf(self,
363 "Detach of the driver failed with error %d\n",
364 err);
365 }
366 return (EIO);
367 }
368
369 return (0);
370 }
371
372 static int
fsl_ehci_detach(device_t self)373 fsl_ehci_detach(device_t self)
374 {
375 int err;
376 ehci_softc_t *sc;
377
378 /* During module unload there are lots of children leftover */
379 err = bus_generic_detach(self);
380 if (err != 0)
381 return (err);
382
383 sc = device_get_softc(self);
384 /*
385 * only call ehci_detach() after ehci_init()
386 */
387 if (sc->sc_flags & EHCI_SCFLG_DONEINIT) {
388 ehci_detach(sc);
389 sc->sc_flags &= ~EHCI_SCFLG_DONEINIT;
390 }
391
392 /* Disable interrupts that might have been switched on in ehci_init */
393 if (sc->sc_io_tag && sc->sc_io_hdl)
394 bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl, EHCI_USBINTR, 0);
395
396 if (sc->sc_irq_res && sc->sc_intr_hdl) {
397 err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
398 if (err) {
399 device_printf(self, "Could not tear down irq, %d\n",
400 err);
401 return (err);
402 }
403 sc->sc_intr_hdl = NULL;
404 }
405
406 if (sc->sc_irq_res) {
407 bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
408 sc->sc_irq_res = NULL;
409 }
410
411 if (sc->sc_io_res) {
412 bus_release_resource(self, SYS_RES_MEMORY, 0, sc->sc_io_res);
413 sc->sc_io_res = NULL;
414 sc->sc_io_tag = 0;
415 sc->sc_io_hdl = 0;
416 }
417
418 return (0);
419 }
420