xref: /netbsd-src/sys/dev/cardbus/fwohci_cardbus.c (revision 56bb44cae5b13a6b74792381ba1e6d930b26aa67)
1 /*	$NetBSD: fwohci_cardbus.c,v 1.33 2010/04/19 07:05:15 kiyohara 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: fwohci_cardbus.c,v 1.33 2010/04/19 07:05:15 kiyohara Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/socket.h>
38 #include <sys/device.h>
39 #include <sys/select.h>
40 
41 #include <sys/bus.h>
42 
43 #if defined pciinc
44 #include <dev/pci/pcidevs.h>
45 #endif
46 
47 #include <dev/cardbus/cardbusvar.h>
48 #include <dev/pci/pcidevs.h>
49 
50 #include <dev/ieee1394/firewire.h>
51 #include <dev/ieee1394/firewirereg.h>
52 #include <dev/ieee1394/fwdma.h>
53 #include <dev/ieee1394/fwohcireg.h>
54 #include <dev/ieee1394/fwohcivar.h>
55 
56 struct fwohci_cardbus_softc {
57 	struct fwohci_softc	sc_sc;
58 	cardbus_chipset_tag_t	sc_cc;
59 	cardbus_function_tag_t	sc_cf;
60 	cardbus_devfunc_t	sc_ct;
61 	void		       *sc_ih;
62 };
63 
64 static int fwohci_cardbus_match(device_t, cfdata_t, void *);
65 static void fwohci_cardbus_attach(device_t, device_t, void *);
66 static int fwohci_cardbus_detach(device_t, int);
67 
68 CFATTACH_DECL_NEW(fwohci_cardbus, sizeof(struct fwohci_cardbus_softc),
69     fwohci_cardbus_match, fwohci_cardbus_attach, fwohci_cardbus_detach, NULL);
70 
71 static int
72 fwohci_cardbus_match(device_t parent, cfdata_t match, void *aux)
73 {
74 	struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
75 
76 	if (PCI_CLASS(ca->ca_class) == PCI_CLASS_SERIALBUS &&
77 	    PCI_SUBCLASS(ca->ca_class) ==
78 	        PCI_SUBCLASS_SERIALBUS_FIREWIRE &&
79 	    PCI_INTERFACE(ca->ca_class) == PCI_INTERFACE_OHCI)
80 		return 1;
81 
82 	return 0;
83 }
84 
85 static void
86 fwohci_cardbus_attach(device_t parent, device_t self, void *aux)
87 {
88 	struct cardbus_attach_args *ca = aux;
89 	struct fwohci_cardbus_softc *sc = device_private(self);
90 	cardbus_devfunc_t ct = ca->ca_ct;
91 	cardbus_chipset_tag_t cc = ct->ct_cc;
92 	cardbus_function_tag_t cf = ct->ct_cf;
93 	pcireg_t csr;
94 	char devinfo[256];
95 
96 	pci_devinfo(ca->ca_id, ca->ca_class, 0, devinfo, sizeof(devinfo));
97 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
98 	       PCI_REVISION(ca->ca_class));
99 	aprint_naive("\n");
100 
101 	/* Map I/O registers */
102 	if (Cardbus_mapreg_map(ct, PCI_OHCI_MAP_REGISTER,
103 	      PCI_MAPREG_TYPE_MEM, 0,
104 	      &sc->sc_sc.bst, &sc->sc_sc.bsh,
105 	      NULL, &sc->sc_sc.bssize)) {
106 		aprint_error_dev(self, "can't map OHCI register space\n");
107 		return;
108 	}
109 
110 	sc->sc_sc.fc.dev = self;
111 	sc->sc_sc.fc.dmat = ca->ca_dmat;
112 	sc->sc_cc = cc;
113 	sc->sc_cf = cf;
114 	sc->sc_ct = ct;
115 
116 	/* Disable interrupts, so we don't get any spurious ones. */
117 	OWRITE(&sc->sc_sc, FWOHCI_INTMASKCLR, OHCI_INT_EN);
118 
119 	/* Enable the device. */
120 	csr = Cardbus_conf_read(ct, ca->ca_tag, PCI_COMMAND_STATUS_REG);
121 	Cardbus_conf_write(ct, ca->ca_tag, PCI_COMMAND_STATUS_REG,
122 	    csr | PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE);
123 
124 	sc->sc_ih = Cardbus_intr_establish(ct, ca->ca_intrline,
125 					   IPL_BIO, fwohci_intr, sc);
126 	if (sc->sc_ih == NULL) {
127 		aprint_error_dev(self, "couldn't establish interrupt\n");
128 		return;
129 	}
130 
131 	/* XXX NULL should be replaced by some call to Cardbus coed */
132 	if (fwohci_init(&sc->sc_sc) != 0) {
133 		Cardbus_intr_disestablish(ct, sc->sc_ih);
134 		sc->sc_ih = NULL;
135 	}
136 }
137 
138 int
139 fwohci_cardbus_detach(device_t self, int flags)
140 {
141 	struct fwohci_cardbus_softc *sc = device_private(self);
142 	cardbus_devfunc_t ct = sc->sc_ct;
143 	int rv;
144 
145 	rv = fwohci_detach(&sc->sc_sc, flags);
146 
147 	if (rv)
148 		return (rv);
149 	if (sc->sc_ih != NULL) {
150 		Cardbus_intr_disestablish(ct, sc->sc_ih);
151 		sc->sc_ih = NULL;
152 	}
153 	if (sc->sc_sc.bssize) {
154 		Cardbus_mapreg_unmap(ct, PCI_OHCI_MAP_REGISTER,
155 			sc->sc_sc.bst, sc->sc_sc.bsh,
156 			sc->sc_sc.bssize);
157 		sc->sc_sc.bssize = 0;
158 	}
159 	return (0);
160 }
161