xref: /netbsd-src/sys/arch/powerpc/booke/dev/pq3ehci.c (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /*	$NetBSD: pq3ehci.c,v 1.2 2011/01/18 01:02:53 matt Exp $	*/
2 /*-
3  * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by Matt Thomas of 3am Software Foundry.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: pq3ehci.c,v 1.2 2011/01/18 01:02:53 matt Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/device.h>
37 #include <sys/kernel.h>
38 #include <sys/proc.h>
39 #include <sys/queue.h>
40 
41 #include <sys/bus.h>
42 
43 #include <powerpc/booke/cpuvar.h>
44 #include <powerpc/booke/e500var.h>
45 #include <powerpc/booke/e500reg.h>
46 
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
49 #include <dev/usb/usbdivar.h>
50 #include <dev/usb/usb_mem.h>
51 
52 #include <dev/usb/ehcireg.h>
53 #include <dev/usb/ehcivar.h>
54 
55 #ifdef EHCI_DEBUG
56 #define DPRINTF(x)	if (ehcidebug) printf x
57 extern int ehcidebug;
58 #else
59 #define DPRINTF(x)
60 #endif
61 
62 static int pq3ehci_match(device_t, cfdata_t, void *);
63 static void pq3ehci_attach(device_t, device_t, void *);
64 
65 struct pq3ehci_softc {
66 	ehci_softc_t		sc;
67 	void 			*sc_ih;		/* interrupt vectoring */
68 };
69 
70 CFATTACH_DECL_NEW(pq3ehci, sizeof(struct pq3ehci_softc),
71     pq3ehci_match, pq3ehci_attach, NULL, NULL);
72 
73 static int
74 pq3ehci_match(device_t parent, cfdata_t cf, void *aux)
75 {
76 
77         if (!e500_cpunode_submatch(parent, cf, cf->cf_name, aux))
78                 return 0;
79 
80         return 1;
81 }
82 
83 static void
84 pq3ehci_attach(device_t parent, device_t self, void *aux)
85 {
86 	struct cpunode_softc * const psc = device_private(parent);
87 	struct pq3ehci_softc * const sc = device_private(self);
88 	struct cpunode_attach_args * const cna = aux;
89 	struct cpunode_locators * const cnl = &cna->cna_locs;
90 	int error;
91 
92 	psc->sc_children |= cna->cna_childmask;
93 	sc->sc.iot = cna->cna_memt;
94 	sc->sc.sc_dev = self;
95 	sc->sc.sc_bus.dmatag = cna->cna_dmat;
96 	sc->sc.sc_bus.hci_private = sc;
97 	sc->sc.sc_bus.usbrev = USBREV_2_0;
98 	sc->sc.sc_ncomp = 0;
99 	sc->sc.sc_flags |= EHCIF_ETTF;
100 
101 	aprint_naive(": USB controller\n");
102 	aprint_normal(": USB controller\n");
103 
104 	error = bus_space_map(sc->sc.iot, cnl->cnl_addr, cnl->cnl_size, 0,
105 	    &sc->sc.ioh);
106 	if (error) {
107 		aprint_error_dev(self,
108 		    "can't map registers for %s#%u: %d\n",
109 		    cnl->cnl_name, cnl->cnl_instance, error);
110 		return;
111 	}
112 	sc->sc.sc_size = cnl->cnl_size;
113 
114 	sc->sc_ih = intr_establish(cnl->cnl_intrs[0], IPL_VM, IST_ONCHIP,
115 	    ehci_intr, sc);
116 	if (sc->sc_ih == NULL) {
117 		aprint_error_dev(self, "failed to establish interrupt %d\n",
118 		     cnl->cnl_intrs[0]);
119 		goto fail;
120 	}
121 	aprint_normal_dev(self, "interrupting on irq %d\n",
122 	     cnl->cnl_intrs[0]);
123 
124 	/* offs is needed for EOWRITEx */
125 	sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH);
126 
127 	/* Disable interrupts, so we don't get any spurious ones. */
128 	DPRINTF(("%s: offs=%d\n", device_xname(self), sc->sc.sc_offs));
129 	EOWRITE2(&sc->sc, EHCI_USBINTR, 0);
130 
131 	error = ehci_init(&sc->sc);
132 	if (error != USBD_NORMAL_COMPLETION) {
133 		aprint_error_dev(self, "init failed, error=%d\n", error);
134 		goto fail;
135 	}
136 
137 	/* Attach usb device. */
138 	sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
139 	return;
140 
141 fail:
142 	if (sc->sc_ih) {
143 		intr_disestablish(sc->sc_ih);
144 		sc->sc_ih = NULL;
145 	}
146 	if (sc->sc.sc_size) {
147 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
148 		sc->sc.sc_size = 0;
149 	}
150 	return;
151 }
152