xref: /netbsd-src/sys/arch/powerpc/booke/dev/pq3ehci.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: pq3ehci.c,v 1.5 2012/07/20 02:14:01 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.5 2012/07/20 02:14:01 matt Exp $");
33 
34 #include "opt_usb.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/queue.h>
42 
43 #include <sys/bus.h>
44 
45 #include <powerpc/booke/cpuvar.h>
46 #include <powerpc/booke/e500var.h>
47 #include <powerpc/booke/e500reg.h>
48 
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdivar.h>
52 #include <dev/usb/usb_mem.h>
53 
54 #include <dev/usb/ehcireg.h>
55 #include <dev/usb/ehcivar.h>
56 
57 #ifdef EHCI_DEBUG
58 #define DPRINTF(x)	if (ehcidebug) printf x
59 extern int ehcidebug;
60 #else
61 #define DPRINTF(x)
62 #endif
63 
64 static int pq3ehci_match(device_t, cfdata_t, void *);
65 static void pq3ehci_attach(device_t, device_t, void *);
66 
67 struct pq3ehci_softc {
68 	ehci_softc_t		sc;
69 	void 			*sc_ih;		/* interrupt vectoring */
70 };
71 
72 CFATTACH_DECL_NEW(pq3ehci, sizeof(struct pq3ehci_softc),
73     pq3ehci_match, pq3ehci_attach, NULL, NULL);
74 
75 static int
76 pq3ehci_match(device_t parent, cfdata_t cf, void *aux)
77 {
78 
79         if (!e500_cpunode_submatch(parent, cf, cf->cf_name, aux))
80                 return 0;
81 
82         return 1;
83 }
84 
85 static void
86 pq3ehci_attach(device_t parent, device_t self, void *aux)
87 {
88 	struct cpunode_softc * const psc = device_private(parent);
89 	struct pq3ehci_softc * const sc = device_private(self);
90 	struct cpunode_attach_args * const cna = aux;
91 	struct cpunode_locators * const cnl = &cna->cna_locs;
92 	int error;
93 
94 	psc->sc_children |= cna->cna_childmask;
95 	sc->sc.iot = cna->cna_le_memt;	/* EHCI registers are little endian */
96 	sc->sc.sc_dev = self;
97 	sc->sc.sc_bus.dmatag = cna->cna_dmat;
98 	sc->sc.sc_bus.hci_private = sc;
99 	sc->sc.sc_bus.usbrev = USBREV_2_0;
100 	sc->sc.sc_ncomp = 0;
101 	sc->sc.sc_flags |= EHCIF_ETTF;
102 
103 	aprint_naive(": USB controller\n");
104 	aprint_normal(": USB controller\n");
105 
106 	error = bus_space_map(sc->sc.iot, cnl->cnl_addr, cnl->cnl_size, 0,
107 	    &sc->sc.ioh);
108 	if (error) {
109 		aprint_error_dev(self,
110 		    "can't map registers for %s#%u: %d\n",
111 		    cnl->cnl_name, cnl->cnl_instance, error);
112 		return;
113 	}
114 	sc->sc.sc_size = cnl->cnl_size;
115 
116 	/*
117 	 * We need to tell the USB interface to snoop all off RAM starting
118 	 * at 0.  Since it can do it by powers of 2, get the highest RAM
119 	 * address and roughly round it to the next power of 2 and find
120 	 * the number of leading zero bits.
121 	 */
122 	cpu_write_4(cnl->cnl_addr + USB_SNOOP1,
123 	    SNOOP_2GB - __builtin_clz(curcpu()->ci_softc->cpu_highmem * 2 - 1));
124 	cpu_write_4(cnl->cnl_addr + USB_CONTROL, USB_EN);
125 
126 	sc->sc_ih = intr_establish(cnl->cnl_intrs[0], IPL_USB, IST_ONCHIP,
127 	    ehci_intr, sc);
128 	if (sc->sc_ih == NULL) {
129 		aprint_error_dev(self, "failed to establish interrupt %d\n",
130 		     cnl->cnl_intrs[0]);
131 		goto fail;
132 	}
133 	aprint_normal_dev(self, "interrupting on irq %d\n",
134 	     cnl->cnl_intrs[0]);
135 
136 	/* offs is needed for EOWRITEx */
137 	sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH);
138 
139 	/* Disable interrupts, so we don't get any spurious ones. */
140 	DPRINTF(("%s: offs=%d\n", device_xname(self), sc->sc.sc_offs));
141 	EOWRITE4(&sc->sc, EHCI_USBINTR, 0);
142 
143 	error = ehci_init(&sc->sc);
144 	if (error != USBD_NORMAL_COMPLETION) {
145 		aprint_error_dev(self, "init failed, error=%d\n", error);
146 		goto fail;
147 	}
148 
149 	/* Attach usb device. */
150 	sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
151 	return;
152 
153 fail:
154 	if (sc->sc_ih) {
155 		intr_disestablish(sc->sc_ih);
156 		sc->sc_ih = NULL;
157 	}
158 	if (sc->sc.sc_size) {
159 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
160 		sc->sc.sc_size = 0;
161 	}
162 	return;
163 }
164