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