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