exynos5_ehci.c revision 258780
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/ehci_exynos5.c 258780 2013-11-30 22:17:27Z eadler $");
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
40#include <dev/ofw/ofw_bus.h>
41#include <dev/ofw/ofw_bus_subr.h>
42
43#include <dev/usb/usb.h>
44#include <dev/usb/usbdi.h>
45#include <dev/usb/usb_busdma.h>
46#include <dev/usb/usb_process.h>
47#include <dev/usb/usb_controller.h>
48#include <dev/usb/usb_bus.h>
49#include <dev/usb/controller/ehci.h>
50#include <dev/usb/controller/ehcireg.h>
51
52#include <dev/fdt/fdt_common.h>
53
54#include <machine/bus.h>
55#include <machine/resource.h>
56
57#include "opt_platform.h"
58
59/* GPIO control */
60#define	GPIO_CON(x, v)	((v) << ((x) * 4))
61#define	GPIO_MASK	0xf
62#define	GPIO_OUTPUT	1
63#define	GPIO_INPUT	0
64#define	GPX3CON		0x0C60
65#define	GPX3DAT		0x0C64
66#define	PIN_USB		5
67
68/* PWR control */
69#define	EXYNOS5_PWR_USBHOST_PHY		0x708
70#define	PHY_POWER_ON			1
71#define	PHY_POWER_OFF			0
72
73/* SYSREG */
74#define	EXYNOS5_SYSREG_USB2_PHY	0x230
75#define	USB2_MODE_HOST		0x1
76
77/* USB HOST */
78#define	HOST_CTRL_CLK_24MHZ	(5 << 16)
79#define	HOST_CTRL_CLK_MASK	(7 << 16)
80#define	HOST_CTRL_SIDDQ		(1 << 6)
81#define	HOST_CTRL_SLEEP		(1 << 5)
82#define	HOST_CTRL_SUSPEND	(1 << 4)
83#define	HOST_CTRL_RESET_LINK	(1 << 1)
84#define	HOST_CTRL_RESET_PHY	(1 << 0)
85#define	HOST_CTRL_RESET_PHY_ALL	(1U << 31)
86
87/* Forward declarations */
88static int	exynos_ehci_attach(device_t dev);
89static int	exynos_ehci_detach(device_t dev);
90static int	exynos_ehci_probe(device_t dev);
91
92struct exynos_ehci_softc {
93	ehci_softc_t		base;
94	struct resource		*res[6];
95	bus_space_tag_t		host_bst;
96	bus_space_tag_t		pwr_bst;
97	bus_space_tag_t		sysreg_bst;
98	bus_space_tag_t		gpio_bst;
99	bus_space_handle_t	host_bsh;
100	bus_space_handle_t	pwr_bsh;
101	bus_space_handle_t	sysreg_bsh;
102	bus_space_handle_t	gpio_bsh;
103
104};
105
106static struct resource_spec exynos_ehci_spec[] = {
107	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
108	{ SYS_RES_MEMORY,	1,	RF_ACTIVE },
109	{ SYS_RES_MEMORY,	2,	RF_ACTIVE },
110	{ SYS_RES_MEMORY,	3,	RF_ACTIVE },
111	{ SYS_RES_MEMORY,	4,	RF_ACTIVE },
112	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
113	{ -1, 0 }
114};
115
116static device_method_t ehci_methods[] = {
117	/* Device interface */
118	DEVMETHOD(device_probe, exynos_ehci_probe),
119	DEVMETHOD(device_attach, exynos_ehci_attach),
120	DEVMETHOD(device_detach, exynos_ehci_detach),
121	DEVMETHOD(device_suspend, bus_generic_suspend),
122	DEVMETHOD(device_resume, bus_generic_resume),
123	DEVMETHOD(device_shutdown, bus_generic_shutdown),
124
125	/* Bus interface */
126	DEVMETHOD(bus_print_child, bus_generic_print_child),
127
128	{ 0, 0 }
129};
130
131/* kobj_class definition */
132static driver_t ehci_driver = {
133	"ehci",
134	ehci_methods,
135	sizeof(ehci_softc_t)
136};
137
138static devclass_t ehci_devclass;
139
140DRIVER_MODULE(ehci, simplebus, ehci_driver, ehci_devclass, 0, 0);
141MODULE_DEPEND(ehci, usb, 1, 1, 1);
142
143/*
144 * Public methods
145 */
146static int
147exynos_ehci_probe(device_t dev)
148{
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	int reg;
161
162	/* Power control */
163	reg = bus_space_read_4(esc->gpio_bst, esc->gpio_bsh, GPX3DAT);
164	reg &= ~(1 << PIN_USB);
165	reg |= (power << PIN_USB);
166	bus_space_write_4(esc->gpio_bst, esc->gpio_bsh, GPX3DAT, reg);
167
168	/* Input/Output control */
169	reg = bus_space_read_4(esc->gpio_bst, esc->gpio_bsh, GPX3CON);
170	reg &= ~GPIO_CON(PIN_USB, GPIO_MASK);
171	reg |= GPIO_CON(PIN_USB, dir);
172	bus_space_write_4(esc->gpio_bst, esc->gpio_bsh, GPX3CON, reg);
173
174	return (0);
175}
176
177static int
178phy_init(struct exynos_ehci_softc *esc)
179{
180	int reg;
181
182	gpio_ctrl(esc, GPIO_INPUT, 1);
183
184	/* set USB HOST mode */
185	bus_space_write_4(esc->sysreg_bst, esc->sysreg_bsh,
186	    EXYNOS5_SYSREG_USB2_PHY, USB2_MODE_HOST);
187
188	/* Power ON phy */
189	bus_space_write_4(esc->pwr_bst, esc->pwr_bsh,
190	    EXYNOS5_PWR_USBHOST_PHY, PHY_POWER_ON);
191
192	reg = bus_space_read_4(esc->host_bst, esc->host_bsh, 0x0);
193	reg &= ~(HOST_CTRL_CLK_MASK |
194	    HOST_CTRL_RESET_PHY |
195	    HOST_CTRL_RESET_PHY_ALL |
196	    HOST_CTRL_SIDDQ |
197	    HOST_CTRL_SUSPEND |
198	    HOST_CTRL_SLEEP);
199
200	reg |= (HOST_CTRL_CLK_24MHZ |
201	    HOST_CTRL_RESET_LINK);
202	bus_space_write_4(esc->host_bst, esc->host_bsh, 0x0, reg);
203
204	DELAY(10);
205
206	reg = bus_space_read_4(esc->host_bst, esc->host_bsh, 0x0);
207	reg &= ~(HOST_CTRL_RESET_LINK);
208	bus_space_write_4(esc->host_bst, esc->host_bsh, 0x0, reg);
209
210	gpio_ctrl(esc, GPIO_OUTPUT, 1);
211
212	return (0);
213}
214
215static int
216exynos_ehci_attach(device_t dev)
217{
218	struct exynos_ehci_softc *esc;
219	ehci_softc_t *sc;
220	bus_space_handle_t bsh;
221	int err;
222
223	esc = device_get_softc(dev);
224	sc = &esc->base;
225	sc->sc_bus.parent = dev;
226	sc->sc_bus.devices = sc->sc_devices;
227	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
228
229	if (bus_alloc_resources(dev, exynos_ehci_spec, esc->res)) {
230		device_printf(dev, "could not allocate resources\n");
231		return (ENXIO);
232	}
233
234	/* EHCI registers */
235	sc->sc_io_tag = rman_get_bustag(esc->res[0]);
236	bsh = rman_get_bushandle(esc->res[0]);
237	sc->sc_io_size = rman_get_size(esc->res[0]);
238
239	/* EHCI HOST ctrl registers */
240	esc->host_bst = rman_get_bustag(esc->res[1]);
241	esc->host_bsh = rman_get_bushandle(esc->res[1]);
242
243	/* PWR registers */
244	esc->pwr_bst = rman_get_bustag(esc->res[2]);
245	esc->pwr_bsh = rman_get_bushandle(esc->res[2]);
246
247	/* SYSREG */
248	esc->sysreg_bst = rman_get_bustag(esc->res[3]);
249	esc->sysreg_bsh = rman_get_bushandle(esc->res[3]);
250
251	/* GPIO */
252	esc->gpio_bst = rman_get_bustag(esc->res[4]);
253	esc->gpio_bsh = rman_get_bushandle(esc->res[4]);
254
255	/* get all DMA memory */
256	if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(dev),
257		&ehci_iterate_hw_softc))
258		return (ENXIO);
259
260	/*
261	 * Set handle to USB related registers subregion used by
262	 * generic EHCI driver.
263	 */
264	err = bus_space_subregion(sc->sc_io_tag, bsh, 0x0,
265	    sc->sc_io_size, &sc->sc_io_hdl);
266	if (err != 0)
267		return (ENXIO);
268
269	phy_init(esc);
270
271	/* Setup interrupt handler */
272	err = bus_setup_intr(dev, esc->res[5], INTR_TYPE_BIO | INTR_MPSAFE,
273	    NULL, (driver_intr_t *)ehci_interrupt, sc,
274	    &sc->sc_intr_hdl);
275	if (err) {
276		device_printf(dev, "Could not setup irq, "
277		    "%d\n", err);
278		return (1);
279	}
280
281	/* Add USB device */
282	sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
283	if (!sc->sc_bus.bdev) {
284		device_printf(dev, "Could not add USB device\n");
285		err = bus_teardown_intr(dev, esc->res[5],
286		    sc->sc_intr_hdl);
287		if (err)
288			device_printf(dev, "Could not tear down irq,"
289			    " %d\n", err);
290		return (1);
291	}
292	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
293
294	strlcpy(sc->sc_vendor, "Samsung", sizeof(sc->sc_vendor));
295
296	err = ehci_init(sc);
297	if (!err) {
298		sc->sc_flags |= EHCI_SCFLG_DONEINIT;
299		err = device_probe_and_attach(sc->sc_bus.bdev);
300	} else {
301		device_printf(dev, "USB init failed err=%d\n", err);
302
303		device_delete_child(dev, sc->sc_bus.bdev);
304		sc->sc_bus.bdev = NULL;
305
306		err = bus_teardown_intr(dev, esc->res[5],
307		    sc->sc_intr_hdl);
308		if (err)
309			device_printf(dev, "Could not tear down irq,"
310			    " %d\n", err);
311		return (1);
312	}
313	return (0);
314}
315
316static int
317exynos_ehci_detach(device_t dev)
318{
319	struct exynos_ehci_softc *esc;
320	ehci_softc_t *sc;
321	int err;
322
323	esc = device_get_softc(dev);
324	sc = &esc->base;
325
326	if (sc->sc_flags & EHCI_SCFLG_DONEINIT)
327		return (0);
328
329	/*
330	 * only call ehci_detach() after ehci_init()
331	 */
332	if (sc->sc_flags & EHCI_SCFLG_DONEINIT) {
333		ehci_detach(sc);
334		sc->sc_flags &= ~EHCI_SCFLG_DONEINIT;
335	}
336
337	/*
338	 * Disable interrupts that might have been switched on in
339	 * ehci_init.
340	 */
341	if (sc->sc_io_tag && sc->sc_io_hdl)
342		bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl,
343		    EHCI_USBINTR, 0);
344
345	if (esc->res[5] && sc->sc_intr_hdl) {
346		err = bus_teardown_intr(dev, esc->res[5],
347		    sc->sc_intr_hdl);
348		if (err) {
349			device_printf(dev, "Could not tear down irq,"
350			    " %d\n", err);
351			return (err);
352		}
353		sc->sc_intr_hdl = NULL;
354	}
355
356	if (sc->sc_bus.bdev) {
357		device_delete_child(dev, sc->sc_bus.bdev);
358		sc->sc_bus.bdev = NULL;
359	}
360
361	/* During module unload there are lots of children leftover */
362	device_delete_children(dev);
363
364	bus_release_resources(dev, exynos_ehci_spec, esc->res);
365
366	return (0);
367}
368