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