xref: /openbsd/sys/dev/pci/if_pgt_pci.c (revision cca36db2)
1 /*	$OpenBSD: if_pgt_pci.c,v 1.13 2010/08/27 20:06:39 deraadt Exp $  */
2 
3 /*
4  * Copyright (c) 2006 Marcus Glocker <mglocker@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * PCI front-end for the PrismGT
21  */
22 
23 #include "bpfilter.h"
24 
25 #include <sys/param.h>
26 #include <sys/sockio.h>
27 #include <sys/mbuf.h>
28 #include <sys/kernel.h>
29 #include <sys/socket.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/timeout.h>
33 #include <sys/device.h>
34 #include <sys/workq.h>
35 
36 #include <machine/bus.h>
37 #include <machine/intr.h>
38 
39 #include <net/if.h>
40 #include <net/if_dl.h>
41 #include <net/if_media.h>
42 
43 #include <netinet/in.h>
44 #include <netinet/if_ether.h>
45 
46 #include <net80211/ieee80211_var.h>
47 #include <net80211/ieee80211_radiotap.h>
48 
49 #include <dev/ic/pgtreg.h>
50 #include <dev/ic/pgtvar.h>
51 
52 #include <dev/pci/pcireg.h>
53 #include <dev/pci/pcivar.h>
54 #include <dev/pci/pcidevs.h>
55 
56 /* Base Address Register */
57 #define PGT_PCI_BAR0	0x10
58 
59 int	pgt_pci_match(struct device *, void *, void *);
60 void	pgt_pci_attach(struct device *, struct device *, void *);
61 int	pgt_pci_detach(struct device *, int);
62 
63 struct pgt_pci_softc {
64 	struct pgt_softc	sc_pgt;
65 
66 	pci_chipset_tag_t       sc_pc;
67 	void 			*sc_ih;
68 	bus_size_t		sc_mapsize;
69 };
70 
71 struct cfattach pgt_pci_ca = {
72 	sizeof(struct pgt_pci_softc), pgt_pci_match, pgt_pci_attach,
73 	pgt_pci_detach, pgt_activate
74 };
75 
76 const struct pci_matchid pgt_pci_devices[] = {
77 	{ PCI_VENDOR_INTERSIL, PCI_PRODUCT_INTERSIL_ISL3877 },
78 	{ PCI_VENDOR_INTERSIL, PCI_PRODUCT_INTERSIL_ISL3890 },
79 	{ PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3CRWE154G72 }
80 };
81 
82 int
83 pgt_pci_match(struct device *parent, void *match, void *aux)
84 {
85 	return (pci_matchbyid((struct pci_attach_args *)aux, pgt_pci_devices,
86 	    sizeof(pgt_pci_devices) / sizeof(pgt_pci_devices[0])));
87 }
88 
89 void
90 pgt_pci_attach(struct device *parent, struct device *self, void *aux)
91 {
92 	struct pgt_pci_softc *psc = (struct pgt_pci_softc *)self;
93 	struct pgt_softc *sc = &psc->sc_pgt;
94 	struct pci_attach_args *pa = aux;
95 	const char *intrstr = NULL;
96 	pci_intr_handle_t ih;
97 	int error;
98 
99 	sc->sc_dmat = pa->pa_dmat;
100 	psc->sc_pc = pa->pa_pc;
101 
102 	/* remember chipset */
103 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTERSIL_ISL3877)
104 		sc->sc_flags |= SC_ISL3877;
105 
106 	/* map control / status registers */
107 	error = pci_mapreg_map(pa, PGT_PCI_BAR0,
108 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
109 	    &sc->sc_iotag, &sc->sc_iohandle, NULL, &psc->sc_mapsize, 0);
110 	if (error != 0) {
111 		printf(": can't map mem space\n");
112 		return;
113 	}
114 
115 	/* map interrupt */
116 	if (pci_intr_map(pa, &ih) != 0) {
117 		printf(": can't map interrupt\n");
118 		return;
119 	}
120 
121 	/* disable all interrupts */
122 	bus_space_write_4(sc->sc_iotag, sc->sc_iohandle, PGT_REG_INT_EN, 0);
123 	(void)bus_space_read_4(sc->sc_iotag, sc->sc_iohandle, PGT_REG_INT_EN);
124 	DELAY(PGT_WRITEIO_DELAY);
125 
126 	/* establish interrupt */
127 	intrstr = pci_intr_string(psc->sc_pc, ih);
128 	psc->sc_ih = pci_intr_establish(psc->sc_pc, ih, IPL_NET, pgt_intr, sc,
129 	    sc->sc_dev.dv_xname);
130 	if (psc->sc_ih == NULL) {
131 		printf(": can't establish interrupt");
132 		if (intrstr != NULL)
133 			printf(" at %s", intrstr);
134 		printf("\n");
135 		return;
136 	}
137 	printf(": %s\n", intrstr);
138 
139 	if (rootvp == NULL)
140 		mountroothook_establish(pgt_attach, sc);
141 	else
142 		pgt_attach(sc);
143 }
144 
145 int
146 pgt_pci_detach(struct device *self, int flags)
147 {
148 	struct pgt_pci_softc *psc = (struct pgt_pci_softc *)self;
149 	struct pgt_softc *sc = &psc->sc_pgt;
150 
151 	if (psc->sc_ih != NULL) {
152 		pgt_detach(sc);
153 		pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
154 	}
155 	if (psc->sc_mapsize > 0)
156 		bus_space_unmap(sc->sc_iotag, sc->sc_iohandle,
157 		    psc->sc_mapsize);
158 
159 	return (0);
160 }
161