a10_ehci.c revision 246057
1/*-
2 * Copyright (c) 2012 Ganbold Tsagaankhuu <ganbold@gmail.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/*
28 * Allwinner A10 attachment driver for the USB Enhanced Host Controller.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/sys/arm/allwinner/a10_ehci.c 246057 2013-01-29 07:21:50Z ganbold $");
33
34#include "opt_bus.h"
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/bus.h>
39#include <sys/rman.h>
40#include <sys/condvar.h>
41#include <sys/kernel.h>
42#include <sys/module.h>
43
44#include <machine/bus.h>
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
51#include <dev/usb/usb_core.h>
52#include <dev/usb/usb_busdma.h>
53#include <dev/usb/usb_process.h>
54#include <dev/usb/usb_util.h>
55
56#include <dev/usb/usb_controller.h>
57#include <dev/usb/usb_bus.h>
58#include <dev/usb/controller/ehci.h>
59#include <dev/usb/controller/ehcireg.h>
60
61#include "a10_clk.h"
62
63#define EHCI_HC_DEVSTR			"Allwinner Integrated USB 2.0 controller"
64
65#define SW_USB_PMU_IRQ_ENABLE		0x800
66
67#define SW_SDRAM_REG_HPCR_USB1		(0x250 + ((1 << 2) * 4))
68#define SW_SDRAM_REG_HPCR_USB2		(0x250 + ((1 << 2) * 5))
69#define SW_SDRAM_BP_HPCR_ACCESS		(1 << 0)
70
71#define SW_ULPI_BYPASS			(1 << 0)
72#define SW_AHB_INCRX_ALIGN		(1 << 8)
73#define	SW_AHB_INCR4			(1 << 9)
74#define SW_AHB_INCR8			(1 << 10)
75
76#define A10_READ_4(sc, reg)		\
77	bus_space_read_4((sc)->sc_io_tag, (sc)->sc_io_hdl, reg)
78
79#define A10_WRITE_4(sc, reg, data)	\
80	bus_space_write_4((sc)->sc_io_tag, (sc)->sc_io_hdl, reg, data)
81
82static device_attach_t a10_ehci_attach;
83static device_detach_t a10_ehci_detach;
84
85bs_r_1_proto(reversed);
86bs_w_1_proto(reversed);
87
88static int
89a10_ehci_probe(device_t self)
90{
91	if (!ofw_bus_is_compatible(self, "allwinner,usb-ehci"))
92		return (ENXIO);
93
94	device_set_desc(self, EHCI_HC_DEVSTR);
95
96	return (BUS_PROBE_DEFAULT);
97}
98
99static int
100a10_ehci_attach(device_t self)
101{
102	ehci_softc_t *sc = device_get_softc(self);
103	bus_space_handle_t bsh;
104	int err;
105	int rid;
106	uint32_t reg_value = 0;
107
108	/* initialise some bus fields */
109	sc->sc_bus.parent = self;
110	sc->sc_bus.devices = sc->sc_devices;
111	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
112
113	/* get all DMA memory */
114	if (usb_bus_mem_alloc_all(&sc->sc_bus,
115	    USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
116		return (ENOMEM);
117	}
118
119	sc->sc_bus.usbrev = USB_REV_2_0;
120
121	rid = 0;
122	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
123	if (!sc->sc_io_res) {
124		device_printf(self, "Could not map memory\n");
125		goto error;
126	}
127
128	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
129	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
130	bsh = rman_get_bushandle(sc->sc_io_res);
131
132	sc->sc_io_size = rman_get_size(sc->sc_io_res);
133
134	if (bus_space_subregion(sc->sc_io_tag, bsh, 0x00,
135	    sc->sc_io_size, &sc->sc_io_hdl) != 0)
136		panic("%s: unable to subregion USB host registers",
137		    device_get_name(self));
138
139	rid = 0;
140	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
141	    RF_SHAREABLE | RF_ACTIVE);
142	if (sc->sc_irq_res == NULL) {
143		device_printf(self, "Could not allocate irq\n");
144		goto error;
145	}
146	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
147	if (!sc->sc_bus.bdev) {
148		device_printf(self, "Could not add USB device\n");
149		goto error;
150	}
151	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
152	device_set_desc(sc->sc_bus.bdev, EHCI_HC_DEVSTR);
153
154	sprintf(sc->sc_vendor, "Allwinner");
155
156	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
157	    NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
158	if (err) {
159		device_printf(self, "Could not setup irq, %d\n", err);
160		sc->sc_intr_hdl = NULL;
161		goto error;
162	}
163
164	sc->sc_flags |= EHCI_SCFLG_DONTRESET;
165
166	/* Enable clock for USB */
167	a10_clk_usb_activate();
168
169	/* Enable passby */
170	reg_value = A10_READ_4(sc, SW_USB_PMU_IRQ_ENABLE);
171	reg_value |= SW_AHB_INCR8; /* AHB INCR8 enable */
172	reg_value |= SW_AHB_INCR4; /* AHB burst type INCR4 enable */
173	reg_value |= SW_AHB_INCRX_ALIGN; /* AHB INCRX align enable */
174	reg_value |= SW_ULPI_BYPASS; /* ULPI bypass enable */
175	A10_WRITE_4(sc, SW_USB_PMU_IRQ_ENABLE, reg_value);
176
177	/* Configure port */
178	reg_value = A10_READ_4(sc, SW_SDRAM_REG_HPCR_USB2);
179	reg_value |= SW_SDRAM_BP_HPCR_ACCESS;
180	A10_WRITE_4(sc, SW_SDRAM_REG_HPCR_USB2, reg_value);
181
182	err = ehci_init(sc);
183	if (!err) {
184		err = device_probe_and_attach(sc->sc_bus.bdev);
185	}
186	if (err) {
187		device_printf(self, "USB init failed err=%d\n", err);
188		goto error;
189	}
190	return (0);
191
192error:
193	a10_ehci_detach(self);
194	return (ENXIO);
195}
196
197static int
198a10_ehci_detach(device_t self)
199{
200	ehci_softc_t *sc = device_get_softc(self);
201	device_t bdev;
202	int err;
203	uint32_t reg_value = 0;
204
205	if (sc->sc_bus.bdev) {
206		bdev = sc->sc_bus.bdev;
207		device_detach(bdev);
208		device_delete_child(self, bdev);
209	}
210	/* during module unload there are lots of children leftover */
211	device_delete_children(self);
212
213	if (sc->sc_irq_res && sc->sc_intr_hdl) {
214		/*
215		 * only call ehci_detach() after ehci_init()
216		 */
217		ehci_detach(sc);
218
219		err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
220
221		if (err)
222			/* XXX or should we panic? */
223			device_printf(self, "Could not tear down irq, %d\n",
224			    err);
225		sc->sc_intr_hdl = NULL;
226	}
227
228	if (sc->sc_irq_res) {
229		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
230		sc->sc_irq_res = NULL;
231	}
232	if (sc->sc_io_res) {
233		bus_release_resource(self, SYS_RES_MEMORY, 0,
234		    sc->sc_io_res);
235		sc->sc_io_res = NULL;
236	}
237	usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
238
239	/* Disable configure port */
240	reg_value = A10_READ_4(sc, SW_SDRAM_REG_HPCR_USB2);
241	reg_value &= ~SW_SDRAM_BP_HPCR_ACCESS;
242	A10_WRITE_4(sc, SW_SDRAM_REG_HPCR_USB2, reg_value);
243
244	/* Disable passby */
245	reg_value = A10_READ_4(sc, SW_USB_PMU_IRQ_ENABLE);
246	reg_value &= ~SW_AHB_INCR8; /* AHB INCR8 disable */
247	reg_value &= ~SW_AHB_INCR4; /* AHB burst type INCR4 disable */
248	reg_value &= ~SW_AHB_INCRX_ALIGN; /* AHB INCRX align disable */
249	reg_value &= ~SW_ULPI_BYPASS; /* ULPI bypass disable */
250	A10_WRITE_4(sc, SW_USB_PMU_IRQ_ENABLE, reg_value);
251
252	/* Disable clock for USB */
253	a10_clk_usb_deactivate();
254
255	return (0);
256}
257
258static device_method_t ehci_methods[] = {
259	/* Device interface */
260	DEVMETHOD(device_probe, a10_ehci_probe),
261	DEVMETHOD(device_attach, a10_ehci_attach),
262	DEVMETHOD(device_detach, a10_ehci_detach),
263	DEVMETHOD(device_suspend, bus_generic_suspend),
264	DEVMETHOD(device_resume, bus_generic_resume),
265	DEVMETHOD(device_shutdown, bus_generic_shutdown),
266
267	DEVMETHOD_END
268};
269
270static driver_t ehci_driver = {
271	.name = "ehci",
272	.methods = ehci_methods,
273	.size = sizeof(ehci_softc_t),
274};
275
276static devclass_t ehci_devclass;
277
278DRIVER_MODULE(ehci, simplebus, ehci_driver, ehci_devclass, 0, 0);
279MODULE_DEPEND(ehci, usb, 1, 1, 1);
280