ohci_cardbus.c revision 1.2 1 1.2 haya /* $NetBSD: ohci_cardbus.c,v 1.2 1999/10/27 10:04:41 haya Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.1 augustss * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.1 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.1 augustss * by Lennart Augustsson (augustss (at) carlstedt.se) at
9 1.1 augustss * Carlstedt Research & Technology.
10 1.1 augustss *
11 1.1 augustss * Redistribution and use in source and binary forms, with or without
12 1.1 augustss * modification, are permitted provided that the following conditions
13 1.1 augustss * are met:
14 1.1 augustss * 1. Redistributions of source code must retain the above copyright
15 1.1 augustss * notice, this list of conditions and the following disclaimer.
16 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 augustss * notice, this list of conditions and the following disclaimer in the
18 1.1 augustss * documentation and/or other materials provided with the distribution.
19 1.1 augustss * 3. All advertising materials mentioning features or use of this software
20 1.1 augustss * must display the following acknowledgement:
21 1.1 augustss * This product includes software developed by the NetBSD
22 1.1 augustss * Foundation, Inc. and its contributors.
23 1.1 augustss * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 augustss * contributors may be used to endorse or promote products derived
25 1.1 augustss * from this software without specific prior written permission.
26 1.1 augustss *
27 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
38 1.1 augustss */
39 1.1 augustss
40 1.1 augustss /*
41 1.1 augustss * USB Open Host Controller driver.
42 1.1 augustss *
43 1.1 augustss * OHCI spec: http://www.intel.com/design/usb/ohci11d.pdf
44 1.1 augustss * USB spec: http://www.teleport.com/cgi-bin/mailmerge.cgi/~usb/cgiform.tpl
45 1.1 augustss */
46 1.1 augustss
47 1.1 augustss #include <sys/param.h>
48 1.1 augustss #include <sys/systm.h>
49 1.1 augustss #include <sys/kernel.h>
50 1.1 augustss #include <sys/device.h>
51 1.1 augustss #include <sys/proc.h>
52 1.1 augustss #include <sys/queue.h>
53 1.1 augustss
54 1.1 augustss #include <machine/bus.h>
55 1.1 augustss
56 1.1 augustss #if defined pciinc
57 1.1 augustss #include <dev/pci/pcidevs.h>
58 1.1 augustss #endif
59 1.2 haya
60 1.1 augustss #include <dev/cardbus/cardbusvar.h>
61 1.1 augustss #include <dev/cardbus/cardbusdevs.h>
62 1.1 augustss
63 1.1 augustss #include <dev/usb/usb.h>
64 1.1 augustss #include <dev/usb/usbdi.h>
65 1.1 augustss #include <dev/usb/usbdivar.h>
66 1.1 augustss #include <dev/usb/usb_mem.h>
67 1.1 augustss
68 1.1 augustss #include <dev/usb/ohcireg.h>
69 1.1 augustss #include <dev/usb/ohcivar.h>
70 1.1 augustss
71 1.1 augustss int ohci_cardbus_match __P((struct device *, struct cfdata *, void *));
72 1.1 augustss void ohci_cardbus_attach __P((struct device *, struct device *, void *));
73 1.1 augustss int ohci_cardbus_detach __P((device_ptr_t, int));
74 1.1 augustss
75 1.1 augustss struct ohci_cardbus_softc {
76 1.1 augustss ohci_softc_t sc;
77 1.1 augustss cardbus_chipset_tag_t sc_cc;
78 1.1 augustss cardbus_function_tag_t sc_cf;
79 1.1 augustss bus_size_t sc_size;
80 1.1 augustss void *sc_ih; /* interrupt vectoring */
81 1.1 augustss };
82 1.1 augustss
83 1.1 augustss struct cfattach ohci_cardbus_ca = {
84 1.1 augustss sizeof(struct ohci_cardbus_softc), ohci_cardbus_match,
85 1.1 augustss ohci_cardbus_attach, ohci_cardbus_detach, ohci_activate
86 1.1 augustss };
87 1.1 augustss
88 1.1 augustss #define CARDBUS_INTERFACE_OHCI PCI_INTERFACE_OHCI
89 1.1 augustss #define CARDBUS_CBMEM PCI_CBMEM
90 1.1 augustss #define cardbus_findvendor pci_findvendor
91 1.1 augustss #define cardbus_devinfo pci_devinfo
92 1.1 augustss
93 1.1 augustss int
94 1.1 augustss ohci_cardbus_match(parent, match, aux)
95 1.1 augustss struct device *parent;
96 1.1 augustss struct cfdata *match;
97 1.1 augustss void *aux;
98 1.1 augustss {
99 1.1 augustss struct cardbus_attach_args *ca = (struct cardbus_attach_args *) aux;
100 1.1 augustss
101 1.1 augustss if (CARDBUS_CLASS(ca->ca_class) == CARDBUS_CLASS_SERIALBUS &&
102 1.1 augustss CARDBUS_SUBCLASS(ca->ca_class) == CARDBUS_SUBCLASS_SERIALBUS_USB &&
103 1.1 augustss CARDBUS_INTERFACE(ca->ca_class) == CARDBUS_INTERFACE_OHCI)
104 1.1 augustss return (1);
105 1.1 augustss
106 1.1 augustss return (0);
107 1.1 augustss }
108 1.1 augustss
109 1.1 augustss void
110 1.1 augustss ohci_cardbus_attach(parent, self, aux)
111 1.1 augustss struct device *parent;
112 1.1 augustss struct device *self;
113 1.1 augustss void *aux;
114 1.1 augustss {
115 1.1 augustss struct ohci_cardbus_softc *sc = (struct ohci_cardbus_softc *)self;
116 1.1 augustss struct cardbus_attach_args *ca = aux;
117 1.1 augustss cardbus_devfunc_t ct = ca->ca_ct;
118 1.1 augustss cardbus_chipset_tag_t cc = ct->ct_cc;
119 1.1 augustss cardbus_function_tag_t cf = ct->ct_cf;
120 1.1 augustss cardbusreg_t csr;
121 1.1 augustss char devinfo[256];
122 1.1 augustss usbd_status r;
123 1.1 augustss char *vendor;
124 1.1 augustss char *devname = sc->sc.sc_bus.bdev.dv_xname;
125 1.1 augustss
126 1.1 augustss cardbus_devinfo(ca->ca_id, ca->ca_class, 0, devinfo);
127 1.1 augustss printf(": %s (rev. 0x%02x)\n", devinfo,
128 1.1 augustss CARDBUS_REVISION(ca->ca_class));
129 1.1 augustss
130 1.1 augustss /* Map I/O registers */
131 1.2 haya if (Cardbus_mapreg_map(ct, CARDBUS_CBMEM, CARDBUS_MAPREG_TYPE_MEM, 0,
132 1.1 augustss &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc_size)) {
133 1.1 augustss printf("%s: can't map mem space\n", devname);
134 1.1 augustss return;
135 1.1 augustss }
136 1.1 augustss
137 1.1 augustss /* Disable interrupts, so we don't can any spurious ones. */
138 1.1 augustss bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
139 1.1 augustss OHCI_ALL_INTRS);
140 1.1 augustss
141 1.1 augustss sc->sc_cc = cc;
142 1.1 augustss sc->sc_cf = cf;
143 1.1 augustss sc->sc.sc_bus.dmatag = ca->ca_dmat;
144 1.1 augustss
145 1.1 augustss #if rbus
146 1.1 augustss #else
147 1.1 augustss XXX (ct->ct_cf->cardbus_mem_open)(cc, 0, iob, iob + 0x40);
148 1.1 augustss #endif
149 1.1 augustss (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
150 1.1 augustss
151 1.1 augustss /* Enable the device. */
152 1.1 augustss csr = cardbus_conf_read(cc, cf, ca->ca_tag,
153 1.1 augustss CARDBUS_COMMAND_STATUS_REG);
154 1.1 augustss cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_COMMAND_STATUS_REG,
155 1.1 augustss csr | CARDBUS_COMMAND_MASTER_ENABLE
156 1.1 augustss | CARDBUS_COMMAND_MEM_ENABLE);
157 1.1 augustss
158 1.1 augustss sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline,
159 1.1 augustss IPL_USB, ohci_intr, sc);
160 1.1 augustss if (sc->sc_ih == NULL) {
161 1.1 augustss printf("%s: couldn't establish interrupt\n", devname);
162 1.1 augustss return;
163 1.1 augustss }
164 1.1 augustss printf("%s: interrupting at %d\n", devname, ca->ca_intrline);
165 1.1 augustss
166 1.1 augustss /* Figure out vendor for root hub descriptor. */
167 1.1 augustss vendor = cardbus_findvendor(ca->ca_id);
168 1.1 augustss sc->sc.sc_id_vendor = CARDBUS_VENDOR(ca->ca_id);
169 1.1 augustss if (vendor)
170 1.1 augustss strncpy(sc->sc.sc_vendor, vendor,
171 1.1 augustss sizeof(sc->sc.sc_vendor) - 1);
172 1.1 augustss else
173 1.1 augustss sprintf(sc->sc.sc_vendor, "vendor 0x%04x",
174 1.1 augustss CARDBUS_VENDOR(ca->ca_id));
175 1.1 augustss
176 1.1 augustss r = ohci_init(&sc->sc);
177 1.1 augustss if (r != USBD_NORMAL_COMPLETION) {
178 1.1 augustss printf("%s: init failed, error=%d\n", devname, r);
179 1.1 augustss
180 1.1 augustss /* Avoid spurious interrupts. */
181 1.1 augustss cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
182 1.1 augustss sc->sc_ih = 0;
183 1.1 augustss
184 1.1 augustss return;
185 1.1 augustss }
186 1.1 augustss
187 1.1 augustss /* Attach usb device. */
188 1.1 augustss sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
189 1.1 augustss usbctlprint);
190 1.1 augustss }
191 1.1 augustss
192 1.1 augustss int
193 1.1 augustss ohci_cardbus_detach(self, flags)
194 1.1 augustss device_ptr_t self;
195 1.1 augustss int flags;
196 1.1 augustss {
197 1.1 augustss struct ohci_cardbus_softc *sc = (struct ohci_cardbus_softc *)self;
198 1.1 augustss int rv;
199 1.1 augustss
200 1.1 augustss rv = ohci_detach(&sc->sc, flags);
201 1.1 augustss if (rv)
202 1.1 augustss return (rv);
203 1.1 augustss if (sc->sc_ih) {
204 1.1 augustss cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
205 1.1 augustss sc->sc_ih = 0;
206 1.1 augustss }
207 1.1 augustss if (sc->sc_size) {
208 1.1 augustss bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc_size);
209 1.1 augustss sc->sc_size = 0;
210 1.1 augustss }
211 1.1 augustss return (0);
212 1.1 augustss }
213