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