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