xref: /netbsd/sys/dev/cardbus/fwohci_cardbus.c (revision bf9ec67e)
1 /*	$NetBSD: fwohci_cardbus.c,v 1.5 2002/01/26 16:34:28 ichiro Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000, 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Matt Thomas of 3am Software Foundry.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: fwohci_cardbus.c,v 1.5 2002/01/26 16:34:28 ichiro Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/socket.h>
45 #include <sys/device.h>
46 
47 #include <machine/bus.h>
48 
49 #if defined pciinc
50 #include <dev/pci/pcidevs.h>
51 #endif
52 
53 #include <dev/cardbus/cardbusvar.h>
54 #include <dev/cardbus/cardbusdevs.h>
55 
56 #include <dev/ieee1394/ieee1394reg.h>
57 #include <dev/ieee1394/ieee1394var.h>
58 #include <dev/ieee1394/fwohcireg.h>
59 #include <dev/ieee1394/fwohcivar.h>
60 
61 struct fwohci_cardbus_softc {
62 	struct fwohci_softc	sc_sc;
63 	cardbus_chipset_tag_t	sc_cc;
64 	cardbus_function_tag_t	sc_cf;
65 	cardbus_devfunc_t	sc_ct;
66 	void		       *sc_ih;
67 };
68 
69 static int fwohci_cardbus_match(struct device *, struct cfdata *, void *);
70 static void fwohci_cardbus_attach(struct device *, struct device *, void *);
71 static int fwohci_cardbus_detach(struct device *, int);
72 
73 struct cfattach fwohci_cardbus_ca = {
74 	sizeof(struct fwohci_cardbus_softc), fwohci_cardbus_match, fwohci_cardbus_attach,
75 	fwohci_cardbus_detach, fwohci_activate
76 };
77 
78 #define CARDBUS_OHCI_MAP_REGISTER PCI_OHCI_MAP_REGISTER
79 #define CARDBUS_INTERFACE_OHCI PCI_INTERFACE_OHCI
80 #define cardbus_devinfo pci_devinfo
81 
82 static int
83 fwohci_cardbus_match(struct device *parent, struct cfdata *match, void *aux)
84 {
85 	struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
86 
87 	if (CARDBUS_CLASS(ca->ca_class) == CARDBUS_CLASS_SERIALBUS &&
88 	    CARDBUS_SUBCLASS(ca->ca_class) ==
89 	        CARDBUS_SUBCLASS_SERIALBUS_FIREWIRE &&
90 	    CARDBUS_INTERFACE(ca->ca_class) == CARDBUS_INTERFACE_OHCI)
91 		return 1;
92 
93 	return 0;
94 }
95 
96 static void
97 fwohci_cardbus_attach(struct device *parent, struct device *self, void *aux)
98 {
99 	struct cardbus_attach_args *ca = aux;
100 	struct fwohci_cardbus_softc *sc = (struct fwohci_cardbus_softc *)self;
101 	cardbus_devfunc_t ct = ca->ca_ct;
102 	cardbus_chipset_tag_t cc = ct->ct_cc;
103 	cardbus_function_tag_t cf = ct->ct_cf;
104 	cardbusreg_t csr;
105 	char devinfo[256];
106 	const char *devname = self->dv_xname;
107 
108 	cardbus_devinfo(ca->ca_id, ca->ca_class, 0, devinfo);
109 	printf(": %s (rev. 0x%02x)\n", devinfo,
110 	       CARDBUS_REVISION(ca->ca_class));
111 
112 	/* Map I/O registers */
113 	if (Cardbus_mapreg_map(ct, CARDBUS_OHCI_MAP_REGISTER,
114 	      CARDBUS_MAPREG_TYPE_MEM, 0,
115 	      &sc->sc_sc.sc_memt, &sc->sc_sc.sc_memh,
116 	      NULL, &sc->sc_sc.sc_memsize)) {
117 		printf("%s: can't map OHCI register space\n", devname);
118 		return;
119 	}
120 
121 	sc->sc_cc = cc;
122 	sc->sc_cf = cf;
123 	sc->sc_ct = ct;
124 	sc->sc_sc.sc_dmat = ca->ca_dmat;
125 
126 #if rbus
127 #else
128 XXX	(ct->ct_cf->cardbus_mem_open)(cc, 0, iob, iob + 0x40);
129 #endif
130 	(ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
131 	(ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
132 
133 	/* Disable interrupts, so we don't get any spurious ones. */
134 	OHCI_CSR_WRITE(&sc->sc_sc, OHCI_REG_IntMaskClear,
135 	    OHCI_Int_MasterEnable);
136 
137 	/* Enable the device. */
138 	csr = cardbus_conf_read(cc, cf, ca->ca_tag,
139 	    CARDBUS_COMMAND_STATUS_REG);
140 	cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_COMMAND_STATUS_REG,
141 	    csr | CARDBUS_COMMAND_MASTER_ENABLE | CARDBUS_COMMAND_MEM_ENABLE);
142 
143 #if BYTE_ORDER == BIG_ENDIAN
144 	csr = cardbus_conf_read(cc, cf, ca->ca_tag,
145 	      CARDBUS_OHCI_CONTROL_REGISTER);
146 	cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_OHCI_CONTROL_REGISTER,
147 	    csr | CARDBUS_GLOBAL_SWAP_BE);
148 #endif
149 
150 	sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline,
151 					   IPL_BIO, fwohci_intr, sc);
152 	if (sc->sc_ih == NULL) {
153 		printf("%s: couldn't establish interrupt\n", devname);
154 		return;
155 	}
156 	printf("%s: interrupting at %d\n", devname, ca->ca_intrline);
157 
158 	/* XXX NULL should be replaced by some call to Cardbus coed */
159 	if (fwohci_init(&sc->sc_sc, NULL) != 0) {
160 		cardbus_intr_disestablish(cc, cf, sc->sc_ih);
161 		bus_space_unmap(sc->sc_sc.sc_memt, sc->sc_sc.sc_memh,
162 		    sc->sc_sc.sc_memsize);
163 	}
164 }
165 
166 int
167 fwohci_cardbus_detach(struct device *self, int flags)
168 {
169 	struct fwohci_cardbus_softc *sc = (struct fwohci_cardbus_softc *)self;
170 	cardbus_devfunc_t ct = sc->sc_ct;
171 	int rv;
172 
173 	rv = fwohci_detach(&sc->sc_sc, flags);
174 
175 	if (rv)
176 		return (rv);
177 	if (sc->sc_ih != NULL) {
178 		cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, sc->sc_ih);
179 		sc->sc_ih = NULL;
180 	}
181 	if (sc->sc_sc.sc_memsize) {
182 		Cardbus_mapreg_unmap(ct, CARDBUS_OHCI_MAP_REGISTER,
183 			sc->sc_sc.sc_memt, sc->sc_sc.sc_memh,
184 			sc->sc_sc.sc_memsize);
185 		sc->sc_sc.sc_memsize = 0;
186 	}
187 	return (0);
188 }
189