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