xref: /openbsd/sys/arch/loongson/dev/ohci_voyager.c (revision 4bdff4be)
1 /*	$OpenBSD: ohci_voyager.c,v 1.8 2022/04/06 18:59:26 naddy Exp $	*/
2 /*	OpenBSD: ohci_pci.c,v 1.33 2008/06/26 05:42:17 ray Exp 	*/
3 /*	$NetBSD: ohci_pci.c,v 1.23 2002/10/02 16:51:47 thorpej Exp $	*/
4 
5 /*
6  * Copyright (c) 1998 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Lennart Augustsson (lennart@augustsson.net) at
11  * Carlstedt Research & Technology.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * USB Open Host Controller driver.
37  *
38  * OHCI spec: http://www.intel.com/design/usb/ohci11d.pdf
39  * USB spec: http://www.teleport.com/cgi-bin/mailmerge.cgi/~usb/cgiform.tpl
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/device.h>
46 #include <sys/proc.h>
47 #include <sys/queue.h>
48 
49 #include <machine/bus.h>
50 
51 #include <dev/pci/pcivar.h>
52 
53 #include <loongson/dev/voyagerreg.h>
54 #include <loongson/dev/voyagervar.h>
55 
56 #include <dev/usb/usb.h>
57 #include <dev/usb/usbdi.h>
58 #include <dev/usb/usbdivar.h>
59 #include <dev/usb/usb_mem.h>
60 
61 #include <dev/usb/ohcireg.h>
62 #include <dev/usb/ohcivar.h>
63 
64 extern int gdium_revision;
65 
66 int	ohci_voyager_match(struct device *, void *, void *);
67 void	ohci_voyager_attach(struct device *, struct device *, void *);
68 void	ohci_voyager_attach_deferred(struct device *);
69 
70 struct ohci_voyager_softc {
71 	struct ohci_softc	sc;
72 	void 			*sc_ih;
73 };
74 
75 const struct cfattach ohci_voyager_ca = {
76 	sizeof(struct ohci_voyager_softc),
77 	ohci_voyager_match, ohci_voyager_attach, NULL, ohci_activate
78 };
79 
80 int
81 ohci_voyager_match(struct device *parent, void *vcf, void *aux)
82 {
83 	struct voyager_attach_args *vaa = (struct voyager_attach_args *)aux;
84 	struct cfdata *cf = (struct cfdata *)vcf;
85 
86 	return gdium_revision == 0 &&
87 	    strcmp(vaa->vaa_name, cf->cf_driver->cd_name) == 0;
88 }
89 
90 void
91 ohci_voyager_attach(struct device *parent, struct device *self, void *aux)
92 {
93 	struct ohci_voyager_softc *sc = (struct ohci_voyager_softc *)self;
94 	struct voyager_attach_args *vaa = (struct voyager_attach_args *)aux;
95 	struct pci_attach_args *pa = vaa->vaa_pa;
96 	int s;
97 	const char *vendor;
98 	char *devname = sc->sc.sc_bus.bdev.dv_xname;
99 
100 	/* Map I/O registers */
101 	sc->sc.sc_size = VOYAGER_OHCI_SIZE;
102 	sc->sc.iot = vaa->vaa_mmiot;
103 	if (bus_space_subregion(vaa->vaa_mmiot, vaa->vaa_mmioh,
104 	    VOYAGER_OHCI_BASE, VOYAGER_OHCI_SIZE, &sc->sc.ioh) != 0) {
105 		printf(": can't map mem space\n");
106 		return;
107 	}
108 
109 	/* Record what interrupts were enabled by SMM/BIOS. */
110 	sc->sc.sc_intre = bus_space_read_4(sc->sc.iot, sc->sc.ioh,
111 	    OHCI_INTERRUPT_ENABLE);
112 
113 	/* Disable interrupts, so we don't get any spurious ones. */
114 	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
115 	    OHCI_MIE);
116 
117 	sc->sc.sc_bus.dmatag = pa->pa_dmat;
118 
119 	bus_space_barrier(sc->sc.iot, sc->sc.ioh, 0, sc->sc.sc_size,
120 	    BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
121 	bus_space_write_4(sc->sc.iot, sc->sc.ioh,
122 	    OHCI_INTERRUPT_DISABLE, OHCI_MIE);
123 
124 	s = splusb();
125 	/* establish the interrupt. */
126 	sc->sc_ih = voyager_intr_establish(parent, VOYAGER_INTR_USB_HOST,
127 	    IPL_USB, ohci_intr, sc, devname);
128 	if (sc->sc_ih == NULL) {
129 		printf(": couldn't establish interrupt\n");
130 		splx(s);
131 		return;
132 	}
133 	printf(": %s, ", voyager_intr_string(sc->sc_ih));
134 
135 	/* Figure out vendor for root hub descriptor. */
136 	vendor = pci_findvendor(pa->pa_id);
137 	sc->sc.sc_id_vendor = PCI_VENDOR(pa->pa_id);
138 	if (vendor)
139 		strlcpy(sc->sc.sc_vendor, vendor, sizeof (sc->sc.sc_vendor));
140 	else
141 		snprintf(sc->sc.sc_vendor, sizeof (sc->sc.sc_vendor),
142 		    "vendor 0x%04x", PCI_VENDOR(pa->pa_id));
143 
144 	/* Display revision and perform legacy emulation handover. */
145 	if (ohci_checkrev(&sc->sc) != USBD_NORMAL_COMPLETION ||
146 	    ohci_handover(&sc->sc) != USBD_NORMAL_COMPLETION) {
147 		splx(s);
148 		return;
149 	}
150 
151 	/* Ignore interrupts for now */
152 	sc->sc.sc_bus.dying = 1;
153 
154 	config_defer(self, ohci_voyager_attach_deferred);
155 
156 	splx(s);
157 }
158 
159 void
160 ohci_voyager_attach_deferred(struct device *self)
161 {
162 	struct ohci_voyager_softc *sc = (struct ohci_voyager_softc *)self;
163 	usbd_status r;
164 	int s;
165 
166 	s = splusb();
167 
168 	sc->sc.sc_bus.dying = 0;
169 
170 	r = ohci_init(&sc->sc);
171 	if (r != USBD_NORMAL_COMPLETION) {
172 		printf("%s: init failed, error=%d\n",
173 		    sc->sc.sc_bus.bdev.dv_xname, r);
174 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
175 		splx(s);
176 		return;
177 	}
178 
179 	splx(s);
180 
181 	/* Attach usb device. */
182 	config_found(self, &sc->sc.sc_bus, usbctlprint);
183 }
184