if_hme_pci.c revision 1.11 1 1.11 thorpej /* $NetBSD: if_hme_pci.c,v 1.11 2002/09/30 20:37:33 thorpej Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.1 mrg * Copyright (c) 2000 Matthew R. Green
5 1.1 mrg * All rights reserved.
6 1.1 mrg *
7 1.1 mrg * Redistribution and use in source and binary forms, with or without
8 1.1 mrg * modification, are permitted provided that the following conditions
9 1.1 mrg * are met:
10 1.1 mrg * 1. Redistributions of source code must retain the above copyright
11 1.1 mrg * notice, this list of conditions and the following disclaimer.
12 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mrg * notice, this list of conditions and the following disclaimer in the
14 1.1 mrg * documentation and/or other materials provided with the distribution.
15 1.1 mrg * 3. The name of the author may not be used to endorse or promote products
16 1.1 mrg * derived from this software without specific prior written permission.
17 1.1 mrg *
18 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 1.1 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 1.1 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 1.1 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 1.1 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 mrg * SUCH DAMAGE.
29 1.1 mrg */
30 1.1 mrg
31 1.1 mrg /*
32 1.1 mrg * PCI front-end device driver for the HME ethernet device.
33 1.1 mrg */
34 1.9 lukem
35 1.9 lukem #include <sys/cdefs.h>
36 1.11 thorpej __KERNEL_RCSID(0, "$NetBSD: if_hme_pci.c,v 1.11 2002/09/30 20:37:33 thorpej Exp $");
37 1.1 mrg
38 1.1 mrg #include <sys/param.h>
39 1.1 mrg #include <sys/systm.h>
40 1.1 mrg #include <sys/syslog.h>
41 1.1 mrg #include <sys/device.h>
42 1.1 mrg #include <sys/malloc.h>
43 1.1 mrg #include <sys/socket.h>
44 1.1 mrg
45 1.1 mrg #include <net/if.h>
46 1.1 mrg #include <net/if_dl.h>
47 1.1 mrg #include <net/if_ether.h>
48 1.1 mrg #include <net/if_media.h>
49 1.1 mrg
50 1.1 mrg #include <dev/mii/mii.h>
51 1.1 mrg #include <dev/mii/miivar.h>
52 1.1 mrg
53 1.5 augustss #include <machine/intr.h>
54 1.1 mrg
55 1.1 mrg #include <dev/pci/pcivar.h>
56 1.1 mrg #include <dev/pci/pcireg.h>
57 1.1 mrg #include <dev/pci/pcidevs.h>
58 1.1 mrg
59 1.1 mrg #include <dev/ic/hmevar.h>
60 1.1 mrg
61 1.1 mrg struct hme_pci_softc {
62 1.1 mrg struct hme_softc hsc_hme; /* HME device */
63 1.1 mrg bus_space_tag_t hsc_memt;
64 1.1 mrg bus_space_handle_t hsc_memh;
65 1.1 mrg void *hsc_ih;
66 1.1 mrg };
67 1.1 mrg
68 1.1 mrg int hmematch_pci __P((struct device *, struct cfdata *, void *));
69 1.1 mrg void hmeattach_pci __P((struct device *, struct device *, void *));
70 1.1 mrg
71 1.11 thorpej CFATTACH_DECL(hme_pci, sizeof(struct hme_pci_softc),
72 1.11 thorpej hmematch_pci, hmeattach_pci, NULL, NULL)
73 1.1 mrg
74 1.1 mrg int
75 1.1 mrg hmematch_pci(parent, cf, aux)
76 1.1 mrg struct device *parent;
77 1.1 mrg struct cfdata *cf;
78 1.1 mrg void *aux;
79 1.1 mrg {
80 1.1 mrg struct pci_attach_args *pa = aux;
81 1.1 mrg
82 1.1 mrg if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
83 1.1 mrg PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_HMENETWORK)
84 1.1 mrg return (1);
85 1.1 mrg
86 1.1 mrg return (0);
87 1.1 mrg }
88 1.1 mrg
89 1.1 mrg void
90 1.1 mrg hmeattach_pci(parent, self, aux)
91 1.1 mrg struct device *parent, *self;
92 1.1 mrg void *aux;
93 1.1 mrg {
94 1.1 mrg struct pci_attach_args *pa = aux;
95 1.1 mrg struct hme_pci_softc *hsc = (void *)self;
96 1.1 mrg struct hme_softc *sc = &hsc->hsc_hme;
97 1.7 thorpej pci_intr_handle_t ih;
98 1.1 mrg pcireg_t csr;
99 1.1 mrg const char *intrstr;
100 1.1 mrg int type;
101 1.1 mrg
102 1.7 thorpej /* XXX the following declarations should be elsewhere */
103 1.7 thorpej extern void myetheraddr __P((u_char *));
104 1.7 thorpej
105 1.7 thorpej printf(": Sun Happy Meal Ethernet, rev. %d\n",
106 1.7 thorpej PCI_REVISION(pa->pa_class));
107 1.7 thorpej
108 1.1 mrg /*
109 1.1 mrg * enable io/memory-space accesses. this is kinda of gross; but
110 1.1 mrg # the hme comes up with neither IO space enabled, or memory space.
111 1.1 mrg */
112 1.1 mrg if (pa->pa_memt)
113 1.1 mrg pa->pa_flags |= PCI_FLAGS_MEM_ENABLED;
114 1.1 mrg if (pa->pa_iot)
115 1.1 mrg pa->pa_flags |= PCI_FLAGS_IO_ENABLED;
116 1.1 mrg csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
117 1.1 mrg if (pa->pa_memt) {
118 1.1 mrg type = PCI_MAPREG_TYPE_MEM;
119 1.1 mrg csr |= PCI_COMMAND_MEM_ENABLE;
120 1.1 mrg sc->sc_bustag = pa->pa_memt;
121 1.1 mrg } else {
122 1.1 mrg type = PCI_MAPREG_TYPE_IO;
123 1.1 mrg csr |= PCI_COMMAND_IO_ENABLE;
124 1.1 mrg sc->sc_bustag = pa->pa_iot;
125 1.1 mrg }
126 1.1 mrg pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
127 1.1 mrg csr | PCI_COMMAND_MEM_ENABLE);
128 1.1 mrg
129 1.1 mrg sc->sc_dmatag = pa->pa_dmat;
130 1.1 mrg
131 1.2 eeh sc->sc_pci = 1; /* XXXXX should all be done in bus_dma. */
132 1.1 mrg /*
133 1.1 mrg * Map five register banks:
134 1.1 mrg *
135 1.1 mrg * bank 0: HME SEB registers: +0x0000
136 1.1 mrg * bank 1: HME ETX registers: +0x2000
137 1.1 mrg * bank 2: HME ERX registers: +0x4000
138 1.1 mrg * bank 3: HME MAC registers: +0x6000
139 1.1 mrg * bank 4: HME MIF registers: +0x7000
140 1.1 mrg *
141 1.1 mrg */
142 1.1 mrg
143 1.1 mrg #define PCI_HME_BASEADDR 0x10
144 1.1 mrg if (pci_mapreg_map(pa, PCI_HME_BASEADDR, type, 0,
145 1.1 mrg &hsc->hsc_memt, &hsc->hsc_memh, NULL, NULL) != 0)
146 1.1 mrg {
147 1.7 thorpej printf("%s: unable to map device registers\n",
148 1.7 thorpej sc->sc_dev.dv_xname);
149 1.1 mrg return;
150 1.1 mrg }
151 1.1 mrg sc->sc_seb = hsc->hsc_memh;
152 1.7 thorpej if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x2000,
153 1.7 thorpej 0x1000, &sc->sc_etx)) {
154 1.7 thorpej printf("%s: unable to subregion ETX registers\n",
155 1.7 thorpej sc->sc_dev.dv_xname);
156 1.7 thorpej return;
157 1.7 thorpej }
158 1.7 thorpej if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x4000,
159 1.7 thorpej 0x1000, &sc->sc_erx)) {
160 1.7 thorpej printf("%s: unable to subregion ERX registers\n",
161 1.7 thorpej sc->sc_dev.dv_xname);
162 1.7 thorpej return;
163 1.7 thorpej }
164 1.7 thorpej if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x6000,
165 1.7 thorpej 0x1000, &sc->sc_mac)) {
166 1.7 thorpej printf("%s: unable to subregion MAC registers\n",
167 1.7 thorpej sc->sc_dev.dv_xname);
168 1.7 thorpej return;
169 1.7 thorpej }
170 1.7 thorpej if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x7000,
171 1.7 thorpej 0x1000, &sc->sc_mif)) {
172 1.7 thorpej printf("%s: unable to subregion MIF registers\n",
173 1.7 thorpej sc->sc_dev.dv_xname);
174 1.7 thorpej return;
175 1.7 thorpej }
176 1.1 mrg
177 1.1 mrg myetheraddr(sc->sc_enaddr);
178 1.1 mrg
179 1.7 thorpej /*
180 1.7 thorpej * Map and establish our interrupt.
181 1.7 thorpej */
182 1.7 thorpej if (pci_intr_map(pa, &ih) != 0) {
183 1.7 thorpej printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
184 1.7 thorpej return;
185 1.7 thorpej }
186 1.7 thorpej intrstr = pci_intr_string(pa->pa_pc, ih);
187 1.7 thorpej hsc->hsc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET, hme_intr, sc);
188 1.7 thorpej if (hsc->hsc_ih == NULL) {
189 1.7 thorpej printf("%s: unable to establish interrupt",
190 1.7 thorpej sc->sc_dev.dv_xname);
191 1.7 thorpej if (intrstr != NULL)
192 1.7 thorpej printf(" at %s", intrstr);
193 1.7 thorpej printf("\n");
194 1.8 thorpej return;
195 1.7 thorpej }
196 1.7 thorpej printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
197 1.1 mrg
198 1.1 mrg sc->sc_burst = 16; /* XXX */
199 1.1 mrg
200 1.7 thorpej /* Finish off the attach. */
201 1.1 mrg hme_config(sc);
202 1.1 mrg }
203