xref: /openbsd-src/sys/dev/pci/if_pgt_pci.c (revision cb39b41371628601fbe4c618205356d538b9d08a)
1 /*	$OpenBSD: if_pgt_pci.c,v 1.16 2015/03/14 03:38:48 jsg 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 
35 #include <machine/bus.h>
36 
37 #include <net/if.h>
38 #include <net/if_dl.h>
39 #include <net/if_media.h>
40 
41 #include <netinet/in.h>
42 #include <netinet/if_ether.h>
43 
44 #include <net80211/ieee80211_var.h>
45 #include <net80211/ieee80211_radiotap.h>
46 
47 #include <dev/ic/pgtreg.h>
48 #include <dev/ic/pgtvar.h>
49 
50 #include <dev/pci/pcireg.h>
51 #include <dev/pci/pcivar.h>
52 #include <dev/pci/pcidevs.h>
53 
54 /* Base Address Register */
55 #define PGT_PCI_BAR0	0x10
56 
57 int	pgt_pci_match(struct device *, void *, void *);
58 void	pgt_pci_attach(struct device *, struct device *, void *);
59 int	pgt_pci_detach(struct device *, int);
60 
61 struct pgt_pci_softc {
62 	struct pgt_softc	sc_pgt;
63 
64 	pci_chipset_tag_t       sc_pc;
65 	void 			*sc_ih;
66 	bus_size_t		sc_mapsize;
67 };
68 
69 struct cfattach pgt_pci_ca = {
70 	sizeof(struct pgt_pci_softc), pgt_pci_match, pgt_pci_attach,
71 	pgt_pci_detach, pgt_activate
72 };
73 
74 const struct pci_matchid pgt_pci_devices[] = {
75 	{ PCI_VENDOR_INTERSIL, PCI_PRODUCT_INTERSIL_ISL3877 },
76 	{ PCI_VENDOR_INTERSIL, PCI_PRODUCT_INTERSIL_ISL3890 },
77 	{ PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3CRWE154G72 }
78 };
79 
80 int
81 pgt_pci_match(struct device *parent, void *match, void *aux)
82 {
83 	return (pci_matchbyid((struct pci_attach_args *)aux, pgt_pci_devices,
84 	    sizeof(pgt_pci_devices) / sizeof(pgt_pci_devices[0])));
85 }
86 
87 void
88 pgt_pci_attach(struct device *parent, struct device *self, void *aux)
89 {
90 	struct pgt_pci_softc *psc = (struct pgt_pci_softc *)self;
91 	struct pgt_softc *sc = &psc->sc_pgt;
92 	struct pci_attach_args *pa = aux;
93 	const char *intrstr = NULL;
94 	pci_intr_handle_t ih;
95 	int error;
96 
97 	sc->sc_dmat = pa->pa_dmat;
98 	psc->sc_pc = pa->pa_pc;
99 
100 	/* remember chipset */
101 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTERSIL_ISL3877)
102 		sc->sc_flags |= SC_ISL3877;
103 
104 	/* map control / status registers */
105 	error = pci_mapreg_map(pa, PGT_PCI_BAR0,
106 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
107 	    &sc->sc_iotag, &sc->sc_iohandle, NULL, &psc->sc_mapsize, 0);
108 	if (error != 0) {
109 		printf(": can't map mem space\n");
110 		return;
111 	}
112 
113 	/* map interrupt */
114 	if (pci_intr_map(pa, &ih) != 0) {
115 		printf(": can't map interrupt\n");
116 		return;
117 	}
118 
119 	/* disable all interrupts */
120 	bus_space_write_4(sc->sc_iotag, sc->sc_iohandle, PGT_REG_INT_EN, 0);
121 	(void)bus_space_read_4(sc->sc_iotag, sc->sc_iohandle, PGT_REG_INT_EN);
122 	DELAY(PGT_WRITEIO_DELAY);
123 
124 	/* establish interrupt */
125 	intrstr = pci_intr_string(psc->sc_pc, ih);
126 	psc->sc_ih = pci_intr_establish(psc->sc_pc, ih, IPL_NET, pgt_intr, sc,
127 	    sc->sc_dev.dv_xname);
128 	if (psc->sc_ih == NULL) {
129 		printf(": can't establish interrupt");
130 		if (intrstr != NULL)
131 			printf(" at %s", intrstr);
132 		printf("\n");
133 		return;
134 	}
135 	printf(": %s\n", intrstr);
136 
137 	if (rootvp == NULL)
138 		mountroothook_establish(pgt_attach, sc);
139 	else
140 		pgt_attach(sc);
141 }
142 
143 int
144 pgt_pci_detach(struct device *self, int flags)
145 {
146 	struct pgt_pci_softc *psc = (struct pgt_pci_softc *)self;
147 	struct pgt_softc *sc = &psc->sc_pgt;
148 
149 	if (psc->sc_ih != NULL) {
150 		pgt_detach(sc);
151 		pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
152 	}
153 	if (psc->sc_mapsize > 0)
154 		bus_space_unmap(sc->sc_iotag, sc->sc_iohandle,
155 		    psc->sc_mapsize);
156 
157 	return (0);
158 }
159