exynos5_ehci.c revision 266875
1/*-
2 * Copyright (c) 2013 Ruslan Bukin <br@bsdpad.com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/arm/samsung/exynos/exynos5_ehci.c 266875 2014-05-30 07:48:55Z br $");
29
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/condvar.h>
38#include <sys/rman.h>
39#include <sys/gpio.h>
40
41#include <dev/ofw/ofw_bus.h>
42#include <dev/ofw/ofw_bus_subr.h>
43
44#include <dev/usb/usb.h>
45#include <dev/usb/usbdi.h>
46#include <dev/usb/usb_busdma.h>
47#include <dev/usb/usb_process.h>
48#include <dev/usb/usb_controller.h>
49#include <dev/usb/usb_bus.h>
50#include <dev/usb/controller/ehci.h>
51#include <dev/usb/controller/ehcireg.h>
52
53#include <dev/fdt/fdt_common.h>
54
55#include <machine/bus.h>
56#include <machine/resource.h>
57
58#include "gpio_if.h"
59
60#include "opt_platform.h"
61
62/* GPIO control */
63#define	GPIO_OUTPUT	1
64#define	GPIO_INPUT	0
65#define	PIN_USB		161
66
67/* PWR control */
68#define	EXYNOS5_PWR_USBHOST_PHY		0x708
69#define	PHY_POWER_ON			1
70#define	PHY_POWER_OFF			0
71
72/* SYSREG */
73#define	EXYNOS5_SYSREG_USB2_PHY	0x0
74#define	USB2_MODE_HOST		0x1
75
76/* USB HOST */
77#define	HOST_CTRL_CLK_24MHZ	(5 << 16)
78#define	HOST_CTRL_CLK_MASK	(7 << 16)
79#define	HOST_CTRL_SIDDQ		(1 << 6)
80#define	HOST_CTRL_SLEEP		(1 << 5)
81#define	HOST_CTRL_SUSPEND	(1 << 4)
82#define	HOST_CTRL_RESET_LINK	(1 << 1)
83#define	HOST_CTRL_RESET_PHY	(1 << 0)
84#define	HOST_CTRL_RESET_PHY_ALL	(1U << 31)
85
86/* Forward declarations */
87static int	exynos_ehci_attach(device_t dev);
88static int	exynos_ehci_detach(device_t dev);
89static int	exynos_ehci_probe(device_t dev);
90
91struct exynos_ehci_softc {
92	device_t		dev;
93	ehci_softc_t		base;
94	struct resource		*res[5];
95	bus_space_tag_t		host_bst;
96	bus_space_tag_t		pwr_bst;
97	bus_space_tag_t		sysreg_bst;
98	bus_space_handle_t	host_bsh;
99	bus_space_handle_t	pwr_bsh;
100	bus_space_handle_t	sysreg_bsh;
101
102};
103
104static struct resource_spec exynos_ehci_spec[] = {
105	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
106	{ SYS_RES_MEMORY,	1,	RF_ACTIVE },
107	{ SYS_RES_MEMORY,	2,	RF_ACTIVE },
108	{ SYS_RES_MEMORY,	3,	RF_ACTIVE },
109	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
110	{ -1, 0 }
111};
112
113static device_method_t ehci_methods[] = {
114	/* Device interface */
115	DEVMETHOD(device_probe, exynos_ehci_probe),
116	DEVMETHOD(device_attach, exynos_ehci_attach),
117	DEVMETHOD(device_detach, exynos_ehci_detach),
118	DEVMETHOD(device_suspend, bus_generic_suspend),
119	DEVMETHOD(device_resume, bus_generic_resume),
120	DEVMETHOD(device_shutdown, bus_generic_shutdown),
121
122	/* Bus interface */
123	DEVMETHOD(bus_print_child, bus_generic_print_child),
124
125	{ 0, 0 }
126};
127
128/* kobj_class definition */
129static driver_t ehci_driver = {
130	"ehci",
131	ehci_methods,
132	sizeof(ehci_softc_t)
133};
134
135static devclass_t ehci_devclass;
136
137DRIVER_MODULE(ehci, simplebus, ehci_driver, ehci_devclass, 0, 0);
138MODULE_DEPEND(ehci, usb, 1, 1, 1);
139
140/*
141 * Public methods
142 */
143static int
144exynos_ehci_probe(device_t dev)
145{
146
147	if (!ofw_bus_status_okay(dev))
148		return (ENXIO);
149
150	if (ofw_bus_is_compatible(dev, "exynos,usb-ehci") == 0)
151		return (ENXIO);
152
153	device_set_desc(dev, "Exynos integrated USB controller");
154	return (BUS_PROBE_DEFAULT);
155}
156
157static int
158gpio_ctrl(struct exynos_ehci_softc *esc, int dir, int power)
159{
160	device_t gpio_dev;
161
162	/* Get the GPIO device, we need this to give power to USB */
163	gpio_dev = devclass_get_device(devclass_find("gpio"), 0);
164	if (gpio_dev == NULL) {
165		device_printf(esc->dev, "cant find gpio_dev\n");
166		return (1);
167	}
168
169	if (power)
170		GPIO_PIN_SET(gpio_dev, PIN_USB, GPIO_PIN_HIGH);
171	else
172		GPIO_PIN_SET(gpio_dev, PIN_USB, GPIO_PIN_LOW);
173
174	if (dir)
175		GPIO_PIN_SETFLAGS(gpio_dev, PIN_USB, GPIO_PIN_OUTPUT);
176	else
177		GPIO_PIN_SETFLAGS(gpio_dev, PIN_USB, GPIO_PIN_INPUT);
178
179	return (0);
180}
181
182static int
183reset_hsic_hub(struct exynos_ehci_softc *esc, phandle_t hub)
184{
185	device_t gpio_dev;
186	pcell_t pin;
187
188	/* TODO(imax): check that hub is compatible with "smsc,usb3503" */
189	if (!OF_hasprop(hub, "freebsd,reset-gpio")) {
190		device_printf(esc->dev,
191		    "cannot detect reset GPIO pin for HSIC hub\n");
192		return (1);
193	}
194
195	if (OF_getencprop(hub, "freebsd,reset-gpio", &pin, sizeof(pin)) < 0) {
196		device_printf(esc->dev,
197		    "failed to decode reset GPIO pin number for HSIC hub\n");
198		return (1);
199	}
200
201	/* Get the GPIO device, we need this to give power to USB */
202	gpio_dev = devclass_get_device(devclass_find("gpio"), 0);
203	if (gpio_dev == NULL) {
204		device_printf(esc->dev, "cant find gpio_dev\n");
205		return (1);
206	}
207
208	GPIO_PIN_SET(gpio_dev, pin, GPIO_PIN_LOW);
209	DELAY(100);
210	GPIO_PIN_SET(gpio_dev, pin, GPIO_PIN_HIGH);
211
212	return (0);
213}
214
215static int
216phy_init(struct exynos_ehci_softc *esc)
217{
218	int reg;
219	phandle_t hub;
220
221	gpio_ctrl(esc, GPIO_INPUT, 1);
222
223	/* set USB HOST mode */
224	bus_space_write_4(esc->sysreg_bst, esc->sysreg_bsh,
225	    EXYNOS5_SYSREG_USB2_PHY, USB2_MODE_HOST);
226
227	/* Power ON phy */
228	bus_space_write_4(esc->pwr_bst, esc->pwr_bsh,
229	    EXYNOS5_PWR_USBHOST_PHY, PHY_POWER_ON);
230
231	reg = bus_space_read_4(esc->host_bst, esc->host_bsh, 0x0);
232	reg &= ~(HOST_CTRL_CLK_MASK |
233	    HOST_CTRL_RESET_PHY |
234	    HOST_CTRL_RESET_PHY_ALL |
235	    HOST_CTRL_SIDDQ |
236	    HOST_CTRL_SUSPEND |
237	    HOST_CTRL_SLEEP);
238
239	reg |= (HOST_CTRL_CLK_24MHZ |
240	    HOST_CTRL_RESET_LINK);
241	bus_space_write_4(esc->host_bst, esc->host_bsh, 0x0, reg);
242
243	DELAY(10);
244
245	reg = bus_space_read_4(esc->host_bst, esc->host_bsh, 0x0);
246	reg &= ~(HOST_CTRL_RESET_LINK);
247	bus_space_write_4(esc->host_bst, esc->host_bsh, 0x0, reg);
248
249	if ((hub = OF_finddevice("/hsichub")) != 0) {
250		reset_hsic_hub(esc, hub);
251	}
252
253	gpio_ctrl(esc, GPIO_OUTPUT, 1);
254
255	return (0);
256}
257
258static int
259exynos_ehci_attach(device_t dev)
260{
261	struct exynos_ehci_softc *esc;
262	ehci_softc_t *sc;
263	bus_space_handle_t bsh;
264	int err;
265
266	esc = device_get_softc(dev);
267	esc->dev = dev;
268	sc = &esc->base;
269	sc->sc_bus.parent = dev;
270	sc->sc_bus.devices = sc->sc_devices;
271	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
272
273	if (bus_alloc_resources(dev, exynos_ehci_spec, esc->res)) {
274		device_printf(dev, "could not allocate resources\n");
275		return (ENXIO);
276	}
277
278	/* EHCI registers */
279	sc->sc_io_tag = rman_get_bustag(esc->res[0]);
280	bsh = rman_get_bushandle(esc->res[0]);
281	sc->sc_io_size = rman_get_size(esc->res[0]);
282
283	/* EHCI HOST ctrl registers */
284	esc->host_bst = rman_get_bustag(esc->res[1]);
285	esc->host_bsh = rman_get_bushandle(esc->res[1]);
286
287	/* PWR registers */
288	esc->pwr_bst = rman_get_bustag(esc->res[2]);
289	esc->pwr_bsh = rman_get_bushandle(esc->res[2]);
290
291	/* SYSREG */
292	esc->sysreg_bst = rman_get_bustag(esc->res[3]);
293	esc->sysreg_bsh = rman_get_bushandle(esc->res[3]);
294
295	/* get all DMA memory */
296	if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(dev),
297		&ehci_iterate_hw_softc))
298		return (ENXIO);
299
300	/*
301	 * Set handle to USB related registers subregion used by
302	 * generic EHCI driver.
303	 */
304	err = bus_space_subregion(sc->sc_io_tag, bsh, 0x0,
305	    sc->sc_io_size, &sc->sc_io_hdl);
306	if (err != 0)
307		return (ENXIO);
308
309	phy_init(esc);
310
311	/* Setup interrupt handler */
312	err = bus_setup_intr(dev, esc->res[4], INTR_TYPE_BIO | INTR_MPSAFE,
313	    NULL, (driver_intr_t *)ehci_interrupt, sc,
314	    &sc->sc_intr_hdl);
315	if (err) {
316		device_printf(dev, "Could not setup irq, "
317		    "%d\n", err);
318		return (1);
319	}
320
321	/* Add USB device */
322	sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
323	if (!sc->sc_bus.bdev) {
324		device_printf(dev, "Could not add USB device\n");
325		err = bus_teardown_intr(dev, esc->res[4],
326		    sc->sc_intr_hdl);
327		if (err)
328			device_printf(dev, "Could not tear down irq,"
329			    " %d\n", err);
330		return (1);
331	}
332	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
333
334	strlcpy(sc->sc_vendor, "Samsung", sizeof(sc->sc_vendor));
335
336	err = ehci_init(sc);
337	if (!err) {
338		sc->sc_flags |= EHCI_SCFLG_DONEINIT;
339		err = device_probe_and_attach(sc->sc_bus.bdev);
340	} else {
341		device_printf(dev, "USB init failed err=%d\n", err);
342
343		device_delete_child(dev, sc->sc_bus.bdev);
344		sc->sc_bus.bdev = NULL;
345
346		err = bus_teardown_intr(dev, esc->res[4],
347		    sc->sc_intr_hdl);
348		if (err)
349			device_printf(dev, "Could not tear down irq,"
350			    " %d\n", err);
351		return (1);
352	}
353	return (0);
354}
355
356static int
357exynos_ehci_detach(device_t dev)
358{
359	struct exynos_ehci_softc *esc;
360	ehci_softc_t *sc;
361	int err;
362
363	esc = device_get_softc(dev);
364	sc = &esc->base;
365
366	if (sc->sc_flags & EHCI_SCFLG_DONEINIT)
367		return (0);
368
369	/*
370	 * only call ehci_detach() after ehci_init()
371	 */
372	if (sc->sc_flags & EHCI_SCFLG_DONEINIT) {
373		ehci_detach(sc);
374		sc->sc_flags &= ~EHCI_SCFLG_DONEINIT;
375	}
376
377	/*
378	 * Disable interrupts that might have been switched on in
379	 * ehci_init.
380	 */
381	if (sc->sc_io_tag && sc->sc_io_hdl)
382		bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl,
383		    EHCI_USBINTR, 0);
384
385	if (esc->res[4] && sc->sc_intr_hdl) {
386		err = bus_teardown_intr(dev, esc->res[4],
387		    sc->sc_intr_hdl);
388		if (err) {
389			device_printf(dev, "Could not tear down irq,"
390			    " %d\n", err);
391			return (err);
392		}
393		sc->sc_intr_hdl = NULL;
394	}
395
396	if (sc->sc_bus.bdev) {
397		device_delete_child(dev, sc->sc_bus.bdev);
398		sc->sc_bus.bdev = NULL;
399	}
400
401	/* During module unload there are lots of children leftover */
402	device_delete_children(dev);
403
404	bus_release_resources(dev, exynos_ehci_spec, esc->res);
405
406	return (0);
407}
408