1e53470feSOleksandr Tymoshenko /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3af3dc4a7SPedro F. Giffuni *
4e53470feSOleksandr Tymoshenko * Copyright (c) 2011
5e53470feSOleksandr Tymoshenko * Ben Gray <ben.r.gray@gmail.com>.
6e53470feSOleksandr Tymoshenko * All rights reserved.
7e53470feSOleksandr Tymoshenko *
8e53470feSOleksandr Tymoshenko * Redistribution and use in source and binary forms, with or without
9e53470feSOleksandr Tymoshenko * modification, are permitted provided that the following conditions
10e53470feSOleksandr Tymoshenko * are met:
11e53470feSOleksandr Tymoshenko * 1. Redistributions of source code must retain the above copyright
12e53470feSOleksandr Tymoshenko * notice, this list of conditions and the following disclaimer.
13e53470feSOleksandr Tymoshenko * 2. Redistributions in binary form must reproduce the above copyright
14e53470feSOleksandr Tymoshenko * notice, this list of conditions and the following disclaimer in the
15e53470feSOleksandr Tymoshenko * documentation and/or other materials provided with the distribution.
16e53470feSOleksandr Tymoshenko *
17e53470feSOleksandr Tymoshenko * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18e53470feSOleksandr Tymoshenko * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19e53470feSOleksandr Tymoshenko * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20e53470feSOleksandr Tymoshenko * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21e53470feSOleksandr Tymoshenko * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22e53470feSOleksandr Tymoshenko * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23e53470feSOleksandr Tymoshenko * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24e53470feSOleksandr Tymoshenko * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25e53470feSOleksandr Tymoshenko * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26e53470feSOleksandr Tymoshenko * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27e53470feSOleksandr Tymoshenko * SUCH DAMAGE.
28e53470feSOleksandr Tymoshenko */
29fdafd315SWarner Losh
30e53470feSOleksandr Tymoshenko #include <sys/param.h>
31e53470feSOleksandr Tymoshenko #include <sys/systm.h>
325b03aba6SOleksandr Tymoshenko #include <sys/conf.h>
33e53470feSOleksandr Tymoshenko #include <sys/kernel.h>
34e53470feSOleksandr Tymoshenko #include <sys/rman.h>
35e53470feSOleksandr Tymoshenko #include <sys/module.h>
365b03aba6SOleksandr Tymoshenko #include <sys/proc.h>
37e53470feSOleksandr Tymoshenko #include <sys/condvar.h>
38e53470feSOleksandr Tymoshenko
395b03aba6SOleksandr Tymoshenko #include <dev/fdt/simplebus.h>
408917c565SOlivier Houchard #include <dev/fdt/fdt_common.h>
41e53470feSOleksandr Tymoshenko #include <dev/ofw/ofw_bus_subr.h>
42e53470feSOleksandr Tymoshenko
43e53470feSOleksandr Tymoshenko #include <dev/usb/usb.h>
44e53470feSOleksandr Tymoshenko #include <dev/usb/usbdi.h>
45e53470feSOleksandr Tymoshenko
46e53470feSOleksandr Tymoshenko #include <dev/usb/usb_core.h>
47e53470feSOleksandr Tymoshenko #include <dev/usb/usb_busdma.h>
48e53470feSOleksandr Tymoshenko #include <dev/usb/usb_process.h>
49e53470feSOleksandr Tymoshenko #include <dev/usb/usb_util.h>
50e53470feSOleksandr Tymoshenko
51e53470feSOleksandr Tymoshenko #include <dev/usb/usb_controller.h>
52e53470feSOleksandr Tymoshenko #include <dev/usb/usb_bus.h>
53e53470feSOleksandr Tymoshenko #include <dev/usb/controller/ehci.h>
54e53470feSOleksandr Tymoshenko #include <dev/usb/controller/ehcireg.h>
55e53470feSOleksandr Tymoshenko
565b03aba6SOleksandr Tymoshenko #include <machine/bus.h>
57e53470feSOleksandr Tymoshenko
58e53470feSOleksandr Tymoshenko #include <arm/ti/usb/omap_usb.h>
59e53470feSOleksandr Tymoshenko
608917c565SOlivier Houchard #include <arm/ti/omap4/pandaboard/pandaboard.h>
618917c565SOlivier Houchard
625b03aba6SOleksandr Tymoshenko /* EHCI */
635b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_HCCAPBASE 0x0000
645b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_HCSPARAMS 0x0004
655b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_HCCPARAMS 0x0008
665b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_USBCMD 0x0010
675b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_USBSTS 0x0014
685b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_USBINTR 0x0018
695b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_FRINDEX 0x001C
705b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_CTRLDSSEGMENT 0x0020
715b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_PERIODICLISTBASE 0x0024
725b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_ASYNCLISTADDR 0x0028
735b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_CONFIGFLAG 0x0050
745b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_PORTSC(i) (0x0054 + (0x04 * (i)))
755b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_INSNREG00 0x0090
765b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_INSNREG01 0x0094
775b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_INSNREG02 0x0098
785b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_INSNREG03 0x009C
795b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_INSNREG04 0x00A0
805b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_INSNREG05_UTMI 0x00A4
815b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_INSNREG05_ULPI 0x00A4
825b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_INSNREG06 0x00A8
835b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_INSNREG07 0x00AC
845b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_INSNREG08 0x00B0
855b03aba6SOleksandr Tymoshenko
865b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_INSNREG04_DISABLE_UNSUSPEND (1 << 5)
875b03aba6SOleksandr Tymoshenko
885b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_INSNREG05_ULPI_CONTROL_SHIFT 31
895b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_INSNREG05_ULPI_PORTSEL_SHIFT 24
905b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_INSNREG05_ULPI_OPSEL_SHIFT 22
915b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_INSNREG05_ULPI_REGADD_SHIFT 16
925b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_INSNREG05_ULPI_EXTREGADD_SHIFT 8
935b03aba6SOleksandr Tymoshenko #define OMAP_USBHOST_INSNREG05_ULPI_WRDATA_SHIFT 0
945b03aba6SOleksandr Tymoshenko
955b03aba6SOleksandr Tymoshenko #define ULPI_FUNC_CTRL_RESET (1 << 5)
965b03aba6SOleksandr Tymoshenko
975b03aba6SOleksandr Tymoshenko /*-------------------------------------------------------------------------*/
985b03aba6SOleksandr Tymoshenko
995b03aba6SOleksandr Tymoshenko /*
1005b03aba6SOleksandr Tymoshenko * Macros for Set and Clear
1015b03aba6SOleksandr Tymoshenko * See ULPI 1.1 specification to find the registers with Set and Clear offsets
1025b03aba6SOleksandr Tymoshenko */
1035b03aba6SOleksandr Tymoshenko #define ULPI_SET(a) (a + 1)
1045b03aba6SOleksandr Tymoshenko #define ULPI_CLR(a) (a + 2)
1055b03aba6SOleksandr Tymoshenko
1065b03aba6SOleksandr Tymoshenko /*-------------------------------------------------------------------------*/
1075b03aba6SOleksandr Tymoshenko
1085b03aba6SOleksandr Tymoshenko /*
1095b03aba6SOleksandr Tymoshenko * Register Map
1105b03aba6SOleksandr Tymoshenko */
1115b03aba6SOleksandr Tymoshenko #define ULPI_VENDOR_ID_LOW 0x00
1125b03aba6SOleksandr Tymoshenko #define ULPI_VENDOR_ID_HIGH 0x01
1135b03aba6SOleksandr Tymoshenko #define ULPI_PRODUCT_ID_LOW 0x02
1145b03aba6SOleksandr Tymoshenko #define ULPI_PRODUCT_ID_HIGH 0x03
1155b03aba6SOleksandr Tymoshenko #define ULPI_FUNC_CTRL 0x04
1165b03aba6SOleksandr Tymoshenko #define ULPI_IFC_CTRL 0x07
1175b03aba6SOleksandr Tymoshenko #define ULPI_OTG_CTRL 0x0a
1185b03aba6SOleksandr Tymoshenko #define ULPI_USB_INT_EN_RISE 0x0d
1195b03aba6SOleksandr Tymoshenko #define ULPI_USB_INT_EN_FALL 0x10
1205b03aba6SOleksandr Tymoshenko #define ULPI_USB_INT_STS 0x13
1215b03aba6SOleksandr Tymoshenko #define ULPI_USB_INT_LATCH 0x14
1225b03aba6SOleksandr Tymoshenko #define ULPI_DEBUG 0x15
1235b03aba6SOleksandr Tymoshenko #define ULPI_SCRATCH 0x16
1245b03aba6SOleksandr Tymoshenko
1255b03aba6SOleksandr Tymoshenko #define OMAP_EHCI_HC_DEVSTR "TI OMAP USB 2.0 controller"
126e53470feSOleksandr Tymoshenko
127e53470feSOleksandr Tymoshenko struct omap_ehci_softc {
128e53470feSOleksandr Tymoshenko ehci_softc_t base; /* storage for EHCI code */
129e53470feSOleksandr Tymoshenko device_t sc_dev;
130e53470feSOleksandr Tymoshenko };
131e53470feSOleksandr Tymoshenko
132e53470feSOleksandr Tymoshenko static device_attach_t omap_ehci_attach;
133e53470feSOleksandr Tymoshenko static device_detach_t omap_ehci_detach;
134e53470feSOleksandr Tymoshenko
135e53470feSOleksandr Tymoshenko /**
136e53470feSOleksandr Tymoshenko * omap_ehci_read_4 - read a 32-bit value from the EHCI registers
137e53470feSOleksandr Tymoshenko * omap_ehci_write_4 - write a 32-bit value from the EHCI registers
138e53470feSOleksandr Tymoshenko * @sc: omap ehci device context
139e53470feSOleksandr Tymoshenko * @off: byte offset within the register set to read from
140e53470feSOleksandr Tymoshenko * @val: the value to write into the register
141e53470feSOleksandr Tymoshenko *
142e53470feSOleksandr Tymoshenko *
143e53470feSOleksandr Tymoshenko * LOCKING:
144e53470feSOleksandr Tymoshenko * None
145e53470feSOleksandr Tymoshenko *
146e53470feSOleksandr Tymoshenko * RETURNS:
147e53470feSOleksandr Tymoshenko * nothing in case of write function, if read function returns the value read.
148e53470feSOleksandr Tymoshenko */
149e53470feSOleksandr Tymoshenko static inline uint32_t
omap_ehci_read_4(struct omap_ehci_softc * sc,bus_size_t off)150e53470feSOleksandr Tymoshenko omap_ehci_read_4(struct omap_ehci_softc *sc, bus_size_t off)
151e53470feSOleksandr Tymoshenko {
152e53470feSOleksandr Tymoshenko return (bus_read_4(sc->base.sc_io_res, off));
153e53470feSOleksandr Tymoshenko }
1545b03aba6SOleksandr Tymoshenko
155e53470feSOleksandr Tymoshenko static inline void
omap_ehci_write_4(struct omap_ehci_softc * sc,bus_size_t off,uint32_t val)156e53470feSOleksandr Tymoshenko omap_ehci_write_4(struct omap_ehci_softc *sc, bus_size_t off, uint32_t val)
157e53470feSOleksandr Tymoshenko {
158e53470feSOleksandr Tymoshenko bus_write_4(sc->base.sc_io_res, off, val);
159e53470feSOleksandr Tymoshenko }
160e53470feSOleksandr Tymoshenko
161e53470feSOleksandr Tymoshenko /**
162e53470feSOleksandr Tymoshenko * omap_ehci_soft_phy_reset - resets the phy using the reset command
163e53470feSOleksandr Tymoshenko * @isc: omap ehci device context
164e53470feSOleksandr Tymoshenko * @port: port to send the reset over
165e53470feSOleksandr Tymoshenko *
166e53470feSOleksandr Tymoshenko *
167e53470feSOleksandr Tymoshenko * LOCKING:
168e53470feSOleksandr Tymoshenko * none
169e53470feSOleksandr Tymoshenko *
170e53470feSOleksandr Tymoshenko * RETURNS:
171e53470feSOleksandr Tymoshenko * nothing
172e53470feSOleksandr Tymoshenko */
173e53470feSOleksandr Tymoshenko static void
omap_ehci_soft_phy_reset(struct omap_ehci_softc * isc,unsigned int port)174e53470feSOleksandr Tymoshenko omap_ehci_soft_phy_reset(struct omap_ehci_softc *isc, unsigned int port)
175e53470feSOleksandr Tymoshenko {
176e53470feSOleksandr Tymoshenko unsigned long timeout = (hz < 10) ? 1 : ((100 * hz) / 1000);
177e53470feSOleksandr Tymoshenko uint32_t reg;
178e53470feSOleksandr Tymoshenko
179e53470feSOleksandr Tymoshenko reg = ULPI_FUNC_CTRL_RESET
180e53470feSOleksandr Tymoshenko /* FUNCTION_CTRL_SET register */
181e53470feSOleksandr Tymoshenko | (ULPI_SET(ULPI_FUNC_CTRL) << OMAP_USBHOST_INSNREG05_ULPI_REGADD_SHIFT)
182e53470feSOleksandr Tymoshenko /* Write */
183e53470feSOleksandr Tymoshenko | (2 << OMAP_USBHOST_INSNREG05_ULPI_OPSEL_SHIFT)
184e53470feSOleksandr Tymoshenko /* PORTn */
185e53470feSOleksandr Tymoshenko | ((port + 1) << OMAP_USBHOST_INSNREG05_ULPI_PORTSEL_SHIFT)
186e53470feSOleksandr Tymoshenko /* start ULPI access*/
187e53470feSOleksandr Tymoshenko | (1 << OMAP_USBHOST_INSNREG05_ULPI_CONTROL_SHIFT);
188e53470feSOleksandr Tymoshenko
189e53470feSOleksandr Tymoshenko omap_ehci_write_4(isc, OMAP_USBHOST_INSNREG05_ULPI, reg);
190e53470feSOleksandr Tymoshenko
191e53470feSOleksandr Tymoshenko /* Wait for ULPI access completion */
192e53470feSOleksandr Tymoshenko while ((omap_ehci_read_4(isc, OMAP_USBHOST_INSNREG05_ULPI)
193e53470feSOleksandr Tymoshenko & (1 << OMAP_USBHOST_INSNREG05_ULPI_CONTROL_SHIFT))) {
194e53470feSOleksandr Tymoshenko /* Sleep for a tick */
195e53470feSOleksandr Tymoshenko pause("USBPHY_RESET", 1);
196e53470feSOleksandr Tymoshenko
197e53470feSOleksandr Tymoshenko if (timeout-- == 0) {
198e53470feSOleksandr Tymoshenko device_printf(isc->sc_dev, "PHY reset operation timed out\n");
199e53470feSOleksandr Tymoshenko break;
200e53470feSOleksandr Tymoshenko }
201e53470feSOleksandr Tymoshenko }
202e53470feSOleksandr Tymoshenko }
203e53470feSOleksandr Tymoshenko
204e53470feSOleksandr Tymoshenko /**
205e53470feSOleksandr Tymoshenko * omap_ehci_init - initialises the USB host EHCI controller
206e53470feSOleksandr Tymoshenko * @isc: omap ehci device context
207e53470feSOleksandr Tymoshenko *
208e53470feSOleksandr Tymoshenko * This initialisation routine is quite heavily based on the work done by the
209e53470feSOleksandr Tymoshenko * OMAP Linux team (for which I thank them very much). The init sequence is
210e53470feSOleksandr Tymoshenko * almost identical, diverging only for the FreeBSD specifics.
211e53470feSOleksandr Tymoshenko *
212e53470feSOleksandr Tymoshenko * LOCKING:
213e53470feSOleksandr Tymoshenko * none
214e53470feSOleksandr Tymoshenko *
215e53470feSOleksandr Tymoshenko * RETURNS:
216e53470feSOleksandr Tymoshenko * 0 on success, a negative error code on failure.
217e53470feSOleksandr Tymoshenko */
218e53470feSOleksandr Tymoshenko static int
omap_ehci_init(struct omap_ehci_softc * isc)219e53470feSOleksandr Tymoshenko omap_ehci_init(struct omap_ehci_softc *isc)
220e53470feSOleksandr Tymoshenko {
221e53470feSOleksandr Tymoshenko uint32_t reg = 0;
222e53470feSOleksandr Tymoshenko int i;
2235b03aba6SOleksandr Tymoshenko device_t uhh_dev;
224e53470feSOleksandr Tymoshenko
2255b03aba6SOleksandr Tymoshenko uhh_dev = device_get_parent(isc->sc_dev);
226e53470feSOleksandr Tymoshenko device_printf(isc->sc_dev, "Starting TI EHCI USB Controller\n");
227e53470feSOleksandr Tymoshenko
228e53470feSOleksandr Tymoshenko /* Set the interrupt threshold control, it controls the maximum rate at
229e53470feSOleksandr Tymoshenko * which the host controller issues interrupts. We set it to 1 microframe
230e53470feSOleksandr Tymoshenko * at startup - the default is 8 mircoframes (equates to 1ms).
231e53470feSOleksandr Tymoshenko */
232e53470feSOleksandr Tymoshenko reg = omap_ehci_read_4(isc, OMAP_USBHOST_USBCMD);
233e53470feSOleksandr Tymoshenko reg &= 0xff00ffff;
234e53470feSOleksandr Tymoshenko reg |= (1 << 16);
235e53470feSOleksandr Tymoshenko omap_ehci_write_4(isc, OMAP_USBHOST_USBCMD, reg);
236e53470feSOleksandr Tymoshenko
237e53470feSOleksandr Tymoshenko /* Soft reset the PHY using PHY reset command over ULPI */
2385b03aba6SOleksandr Tymoshenko for (i = 0; i < OMAP_HS_USB_PORTS; i++) {
2395b03aba6SOleksandr Tymoshenko if (omap_usb_port_mode(uhh_dev, i) == EHCI_HCD_OMAP_MODE_PHY)
2405b03aba6SOleksandr Tymoshenko omap_ehci_soft_phy_reset(isc, i);
241e53470feSOleksandr Tymoshenko }
242e53470feSOleksandr Tymoshenko
243e53470feSOleksandr Tymoshenko return(0);
244e53470feSOleksandr Tymoshenko }
245e53470feSOleksandr Tymoshenko
246e53470feSOleksandr Tymoshenko /**
247e53470feSOleksandr Tymoshenko * omap_ehci_probe - starts the given command
248e53470feSOleksandr Tymoshenko * @dev:
249e53470feSOleksandr Tymoshenko *
250e53470feSOleksandr Tymoshenko * Effectively boilerplate EHCI resume code.
251e53470feSOleksandr Tymoshenko *
252e53470feSOleksandr Tymoshenko * LOCKING:
253e53470feSOleksandr Tymoshenko * Caller should be holding the OMAP3_MMC lock.
254e53470feSOleksandr Tymoshenko *
255e53470feSOleksandr Tymoshenko * RETURNS:
256e53470feSOleksandr Tymoshenko * EH_HANDLED or EH_NOT_HANDLED
257e53470feSOleksandr Tymoshenko */
258e53470feSOleksandr Tymoshenko static int
omap_ehci_probe(device_t dev)259e53470feSOleksandr Tymoshenko omap_ehci_probe(device_t dev)
260e53470feSOleksandr Tymoshenko {
261add35ed5SIan Lepore if (!ofw_bus_status_okay(dev))
262add35ed5SIan Lepore return (ENXIO);
263add35ed5SIan Lepore
2645b03aba6SOleksandr Tymoshenko if (!ofw_bus_is_compatible(dev, "ti,ehci-omap"))
265e53470feSOleksandr Tymoshenko return (ENXIO);
266e53470feSOleksandr Tymoshenko
267e53470feSOleksandr Tymoshenko device_set_desc(dev, OMAP_EHCI_HC_DEVSTR);
268e53470feSOleksandr Tymoshenko
269e53470feSOleksandr Tymoshenko return (BUS_PROBE_DEFAULT);
270e53470feSOleksandr Tymoshenko }
271e53470feSOleksandr Tymoshenko
272e53470feSOleksandr Tymoshenko /**
273e53470feSOleksandr Tymoshenko * omap_ehci_attach - driver entry point, sets up the ECHI controller/driver
274e53470feSOleksandr Tymoshenko * @dev: the new device handle
275e53470feSOleksandr Tymoshenko *
276e53470feSOleksandr Tymoshenko * Sets up bus spaces, interrupt handles, etc for the EHCI controller. It also
277e53470feSOleksandr Tymoshenko * parses the resource hints and calls omap_ehci_init() to initialise the
278e53470feSOleksandr Tymoshenko * H/W.
279e53470feSOleksandr Tymoshenko *
280e53470feSOleksandr Tymoshenko * LOCKING:
281e53470feSOleksandr Tymoshenko * none
282e53470feSOleksandr Tymoshenko *
283e53470feSOleksandr Tymoshenko * RETURNS:
284e53470feSOleksandr Tymoshenko * 0 on success or a positive error code on failure.
285e53470feSOleksandr Tymoshenko */
286e53470feSOleksandr Tymoshenko static int
omap_ehci_attach(device_t dev)287e53470feSOleksandr Tymoshenko omap_ehci_attach(device_t dev)
288e53470feSOleksandr Tymoshenko {
289e53470feSOleksandr Tymoshenko struct omap_ehci_softc *isc = device_get_softc(dev);
290e53470feSOleksandr Tymoshenko ehci_softc_t *sc = &isc->base;
291487012f4SOlivier Houchard #ifdef SOC_OMAP4
292487012f4SOlivier Houchard phandle_t root;
293487012f4SOlivier Houchard #endif
294e53470feSOleksandr Tymoshenko int err;
295e53470feSOleksandr Tymoshenko int rid;
296e53470feSOleksandr Tymoshenko
297487012f4SOlivier Houchard #ifdef SOC_OMAP4
298487012f4SOlivier Houchard /*
299487012f4SOlivier Houchard * If we're running a Pandaboard, run Pandaboard-specific
300487012f4SOlivier Houchard * init code.
301487012f4SOlivier Houchard */
302487012f4SOlivier Houchard root = OF_finddevice("/");
303487012f4SOlivier Houchard if (ofw_bus_node_is_compatible(root, "ti,omap4-panda"))
304487012f4SOlivier Houchard pandaboard_usb_hub_init();
305487012f4SOlivier Houchard #endif
306487012f4SOlivier Houchard
307e53470feSOleksandr Tymoshenko /* initialise some bus fields */
308e53470feSOleksandr Tymoshenko sc->sc_bus.parent = dev;
309e53470feSOleksandr Tymoshenko sc->sc_bus.devices = sc->sc_devices;
310e53470feSOleksandr Tymoshenko sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
311b217d184SHans Petter Selasky sc->sc_bus.dma_bits = 32;
312e53470feSOleksandr Tymoshenko
3135b03aba6SOleksandr Tymoshenko sprintf(sc->sc_vendor, "Texas Instruments");
3145b03aba6SOleksandr Tymoshenko
315e53470feSOleksandr Tymoshenko /* save the device */
316e53470feSOleksandr Tymoshenko isc->sc_dev = dev;
317e53470feSOleksandr Tymoshenko
318e53470feSOleksandr Tymoshenko /* get all DMA memory */
319e53470feSOleksandr Tymoshenko if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(dev),
320e53470feSOleksandr Tymoshenko &ehci_iterate_hw_softc)) {
321e53470feSOleksandr Tymoshenko return (ENOMEM);
322e53470feSOleksandr Tymoshenko }
323e53470feSOleksandr Tymoshenko
324e53470feSOleksandr Tymoshenko /* Allocate resource for the EHCI register set */
325e53470feSOleksandr Tymoshenko rid = 0;
326e53470feSOleksandr Tymoshenko sc->sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
327e53470feSOleksandr Tymoshenko if (!sc->sc_io_res) {
328e53470feSOleksandr Tymoshenko device_printf(dev, "Error: Could not map EHCI memory\n");
329e53470feSOleksandr Tymoshenko goto error;
330e53470feSOleksandr Tymoshenko }
331e53470feSOleksandr Tymoshenko /* Request an interrupt resource */
332e53470feSOleksandr Tymoshenko rid = 0;
333e53470feSOleksandr Tymoshenko sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
334e53470feSOleksandr Tymoshenko if (sc->sc_irq_res == NULL) {
335e53470feSOleksandr Tymoshenko device_printf(dev, "Error: could not allocate irq\n");
336e53470feSOleksandr Tymoshenko goto error;
337e53470feSOleksandr Tymoshenko }
338e53470feSOleksandr Tymoshenko
339e53470feSOleksandr Tymoshenko /* Add this device as a child of the USBus device */
3405b56413dSWarner Losh sc->sc_bus.bdev = device_add_child(dev, "usbus", DEVICE_UNIT_ANY);
341e53470feSOleksandr Tymoshenko if (!sc->sc_bus.bdev) {
342e53470feSOleksandr Tymoshenko device_printf(dev, "Error: could not add USB device\n");
343e53470feSOleksandr Tymoshenko goto error;
344e53470feSOleksandr Tymoshenko }
345e53470feSOleksandr Tymoshenko
346e53470feSOleksandr Tymoshenko device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
347e53470feSOleksandr Tymoshenko device_set_desc(sc->sc_bus.bdev, OMAP_EHCI_HC_DEVSTR);
348e53470feSOleksandr Tymoshenko
349e53470feSOleksandr Tymoshenko /* Initialise the ECHI registers */
350e53470feSOleksandr Tymoshenko err = omap_ehci_init(isc);
351e53470feSOleksandr Tymoshenko if (err) {
352e53470feSOleksandr Tymoshenko device_printf(dev, "Error: could not setup OMAP EHCI, %d\n", err);
353e53470feSOleksandr Tymoshenko goto error;
354e53470feSOleksandr Tymoshenko }
355e53470feSOleksandr Tymoshenko
356e53470feSOleksandr Tymoshenko /* Set the tag and size of the register set in the EHCI context */
357e53470feSOleksandr Tymoshenko sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
358e53470feSOleksandr Tymoshenko sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
359e53470feSOleksandr Tymoshenko sc->sc_io_size = rman_get_size(sc->sc_io_res);
360e53470feSOleksandr Tymoshenko
361e53470feSOleksandr Tymoshenko /* Setup the interrupt */
362e53470feSOleksandr Tymoshenko err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
363e53470feSOleksandr Tymoshenko NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
364e53470feSOleksandr Tymoshenko if (err) {
365e53470feSOleksandr Tymoshenko device_printf(dev, "Error: could not setup irq, %d\n", err);
366e53470feSOleksandr Tymoshenko sc->sc_intr_hdl = NULL;
367e53470feSOleksandr Tymoshenko goto error;
368e53470feSOleksandr Tymoshenko }
369e53470feSOleksandr Tymoshenko
370e53470feSOleksandr Tymoshenko /* Finally we are ready to kick off the ECHI host controller */
371e53470feSOleksandr Tymoshenko err = ehci_init(sc);
372e53470feSOleksandr Tymoshenko if (err == 0) {
373e53470feSOleksandr Tymoshenko err = device_probe_and_attach(sc->sc_bus.bdev);
374e53470feSOleksandr Tymoshenko }
375e53470feSOleksandr Tymoshenko if (err) {
376e53470feSOleksandr Tymoshenko device_printf(dev, "Error: USB init failed err=%d\n", err);
377e53470feSOleksandr Tymoshenko goto error;
378e53470feSOleksandr Tymoshenko }
379e53470feSOleksandr Tymoshenko
380e53470feSOleksandr Tymoshenko return (0);
381e53470feSOleksandr Tymoshenko
382e53470feSOleksandr Tymoshenko error:
383e53470feSOleksandr Tymoshenko omap_ehci_detach(dev);
384e53470feSOleksandr Tymoshenko return (ENXIO);
385e53470feSOleksandr Tymoshenko }
386e53470feSOleksandr Tymoshenko
387e53470feSOleksandr Tymoshenko /**
388e53470feSOleksandr Tymoshenko * omap_ehci_detach - detach the device and cleanup the driver
389e53470feSOleksandr Tymoshenko * @dev: device handle
390e53470feSOleksandr Tymoshenko *
391e53470feSOleksandr Tymoshenko * Clean-up routine where everything initialised in omap_ehci_attach is
392e53470feSOleksandr Tymoshenko * freed and cleaned up. This function calls omap_ehci_fini() to shutdown
393e53470feSOleksandr Tymoshenko * the on-chip module.
394e53470feSOleksandr Tymoshenko *
395e53470feSOleksandr Tymoshenko * LOCKING:
396e53470feSOleksandr Tymoshenko * none
397e53470feSOleksandr Tymoshenko *
398e53470feSOleksandr Tymoshenko * RETURNS:
399e53470feSOleksandr Tymoshenko * Always returns 0 (success).
400e53470feSOleksandr Tymoshenko */
401e53470feSOleksandr Tymoshenko static int
omap_ehci_detach(device_t dev)402e53470feSOleksandr Tymoshenko omap_ehci_detach(device_t dev)
403e53470feSOleksandr Tymoshenko {
404e53470feSOleksandr Tymoshenko struct omap_ehci_softc *isc = device_get_softc(dev);
405e53470feSOleksandr Tymoshenko ehci_softc_t *sc = &isc->base;
406e53470feSOleksandr Tymoshenko int err;
407e53470feSOleksandr Tymoshenko
408e53470feSOleksandr Tymoshenko /* during module unload there are lots of children leftover */
409*3ddaf820SJohn Baldwin err = bus_generic_detach(dev);
410*3ddaf820SJohn Baldwin if (err != 0)
411*3ddaf820SJohn Baldwin return (err);
412e53470feSOleksandr Tymoshenko
413e53470feSOleksandr Tymoshenko /*
414e53470feSOleksandr Tymoshenko * disable interrupts that might have been switched on in ehci_init
415e53470feSOleksandr Tymoshenko */
416e53470feSOleksandr Tymoshenko if (sc->sc_io_res) {
417e53470feSOleksandr Tymoshenko EWRITE4(sc, EHCI_USBINTR, 0);
418e53470feSOleksandr Tymoshenko }
419e53470feSOleksandr Tymoshenko
420e53470feSOleksandr Tymoshenko if (sc->sc_irq_res && sc->sc_intr_hdl) {
421e53470feSOleksandr Tymoshenko /*
422e53470feSOleksandr Tymoshenko * only call ehci_detach() after ehci_init()
423e53470feSOleksandr Tymoshenko */
424e53470feSOleksandr Tymoshenko ehci_detach(sc);
425e53470feSOleksandr Tymoshenko
426e53470feSOleksandr Tymoshenko err = bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intr_hdl);
427e53470feSOleksandr Tymoshenko if (err)
428e53470feSOleksandr Tymoshenko device_printf(dev, "Error: could not tear down irq, %d\n", err);
429e53470feSOleksandr Tymoshenko sc->sc_intr_hdl = NULL;
430e53470feSOleksandr Tymoshenko }
431e53470feSOleksandr Tymoshenko
432e53470feSOleksandr Tymoshenko /* Free the resources stored in the base EHCI handler */
433e53470feSOleksandr Tymoshenko if (sc->sc_irq_res) {
434e53470feSOleksandr Tymoshenko bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
435e53470feSOleksandr Tymoshenko sc->sc_irq_res = NULL;
436e53470feSOleksandr Tymoshenko }
437e53470feSOleksandr Tymoshenko if (sc->sc_io_res) {
438e53470feSOleksandr Tymoshenko bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_io_res);
439e53470feSOleksandr Tymoshenko sc->sc_io_res = NULL;
440e53470feSOleksandr Tymoshenko }
441e53470feSOleksandr Tymoshenko
442e53470feSOleksandr Tymoshenko return (0);
443e53470feSOleksandr Tymoshenko }
444e53470feSOleksandr Tymoshenko
445e53470feSOleksandr Tymoshenko static device_method_t ehci_methods[] = {
446e53470feSOleksandr Tymoshenko /* Device interface */
447e53470feSOleksandr Tymoshenko DEVMETHOD(device_probe, omap_ehci_probe),
448e53470feSOleksandr Tymoshenko DEVMETHOD(device_attach, omap_ehci_attach),
449e53470feSOleksandr Tymoshenko DEVMETHOD(device_detach, omap_ehci_detach),
4505b03aba6SOleksandr Tymoshenko
4515b03aba6SOleksandr Tymoshenko DEVMETHOD(device_suspend, bus_generic_suspend),
4525b03aba6SOleksandr Tymoshenko DEVMETHOD(device_resume, bus_generic_resume),
4535b03aba6SOleksandr Tymoshenko DEVMETHOD(device_shutdown, bus_generic_shutdown),
454e53470feSOleksandr Tymoshenko
455e53470feSOleksandr Tymoshenko /* Bus interface */
456e53470feSOleksandr Tymoshenko DEVMETHOD(bus_print_child, bus_generic_print_child),
457e53470feSOleksandr Tymoshenko {0, 0}
458e53470feSOleksandr Tymoshenko };
459e53470feSOleksandr Tymoshenko
460e53470feSOleksandr Tymoshenko static driver_t ehci_driver = {
461e53470feSOleksandr Tymoshenko "ehci",
462e53470feSOleksandr Tymoshenko ehci_methods,
463e53470feSOleksandr Tymoshenko sizeof(struct omap_ehci_softc),
464e53470feSOleksandr Tymoshenko };
465e53470feSOleksandr Tymoshenko
4668537e671SJohn Baldwin DRIVER_MODULE(omap_ehci, omap_uhh, ehci_driver, 0, 0);
467