xref: /openbsd/sys/dev/pci/if_epic_pci.c (revision 0f9891f1)
1*0f9891f1Sjsg /*	$OpenBSD: if_epic_pci.c,v 1.18 2024/05/24 06:02:53 jsg Exp $	*/
2059342fcSbrad /*	$NetBSD: if_epic_pci.c,v 1.28 2005/02/27 00:27:32 perry Exp $	*/
3059342fcSbrad 
4059342fcSbrad /*-
5059342fcSbrad  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
6059342fcSbrad  * All rights reserved.
7059342fcSbrad  *
8059342fcSbrad  * This code is derived from software contributed to The NetBSD Foundation
9059342fcSbrad  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10059342fcSbrad  * NASA Ames Research Center.
11059342fcSbrad  *
12059342fcSbrad  * Redistribution and use in source and binary forms, with or without
13059342fcSbrad  * modification, are permitted provided that the following conditions
14059342fcSbrad  * are met:
15059342fcSbrad  * 1. Redistributions of source code must retain the above copyright
16059342fcSbrad  *    notice, this list of conditions and the following disclaimer.
17059342fcSbrad  * 2. Redistributions in binary form must reproduce the above copyright
18059342fcSbrad  *    notice, this list of conditions and the following disclaimer in the
19059342fcSbrad  *    documentation and/or other materials provided with the distribution.
20059342fcSbrad  *
21059342fcSbrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22059342fcSbrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23059342fcSbrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24059342fcSbrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25059342fcSbrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26059342fcSbrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27059342fcSbrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28059342fcSbrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29059342fcSbrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30059342fcSbrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31059342fcSbrad  * POSSIBILITY OF SUCH DAMAGE.
32059342fcSbrad  */
33059342fcSbrad 
34059342fcSbrad /*
35059342fcSbrad  * PCI bus front-end for the Standard Microsystems Corp. 83C170
36059342fcSbrad  * Ethernet PCI Integrated Controller (EPIC/100) driver.
37059342fcSbrad  */
38059342fcSbrad 
39059342fcSbrad #include <sys/param.h>
40059342fcSbrad #include <sys/systm.h>
41059342fcSbrad #include <sys/device.h>
42059342fcSbrad 
43059342fcSbrad #include <net/if.h>
4432f46ff2Smpi #include <net/if_media.h>
45059342fcSbrad 
46059342fcSbrad #include <netinet/in.h>
47059342fcSbrad #include <netinet/if_ether.h>
48059342fcSbrad 
49059342fcSbrad #include <machine/bus.h>
50059342fcSbrad #include <machine/intr.h>
51059342fcSbrad 
52059342fcSbrad #include <dev/mii/miivar.h>
53059342fcSbrad 
54059342fcSbrad #include <dev/ic/smc83c170reg.h>
55059342fcSbrad #include <dev/ic/smc83c170var.h>
56059342fcSbrad 
57059342fcSbrad #include <dev/pci/pcivar.h>
58059342fcSbrad #include <dev/pci/pcireg.h>
59059342fcSbrad #include <dev/pci/pcidevs.h>
60059342fcSbrad 
61059342fcSbrad /*
62059342fcSbrad  * PCI configuration space registers used by the EPIC.
63059342fcSbrad  */
64059342fcSbrad #define	EPIC_PCI_IOBA		0x10	/* i/o mapped base */
65059342fcSbrad #define	EPIC_PCI_MMBA		0x14	/* memory mapped base */
66059342fcSbrad 
67059342fcSbrad struct epic_pci_softc {
68059342fcSbrad 	struct epic_softc sc_epic;	/* real EPIC softc */
69059342fcSbrad 
70059342fcSbrad 	/* PCI-specific goo. */
71059342fcSbrad 	void	*sc_ih;			/* interrupt handle */
72059342fcSbrad };
73059342fcSbrad 
74d0d7b0d0Sbrad int	epic_pci_match(struct device *, void *, void *);
75d0d7b0d0Sbrad void	epic_pci_attach(struct device *, struct device *, void *);
76059342fcSbrad 
778d2c75e4Smpi const struct cfattach epic_pci_ca = {
78059342fcSbrad 	sizeof(struct epic_pci_softc), epic_pci_match, epic_pci_attach
79059342fcSbrad };
80059342fcSbrad 
810ff4d878Sbrad const struct pci_matchid epic_pci_devices[] = {
820ff4d878Sbrad 	{ PCI_VENDOR_SMC, PCI_PRODUCT_SMC_83C170 },
830ff4d878Sbrad 	{ PCI_VENDOR_SMC, PCI_PRODUCT_SMC_83C175 },
84059342fcSbrad };
85059342fcSbrad 
86059342fcSbrad static const struct epic_pci_subsys_info {
87059342fcSbrad 	pcireg_t subsysid;
88059342fcSbrad 	int flags;
89059342fcSbrad } epic_pci_subsys_info[] = {
90059342fcSbrad 	{ PCI_ID_CODE(PCI_VENDOR_SMC, 0xa015), /* SMC9432BTX */
91059342fcSbrad 	  EPIC_HAS_BNC },
92059342fcSbrad 	{ PCI_ID_CODE(PCI_VENDOR_SMC, 0xa024), /* SMC9432BTX1 */
93059342fcSbrad 	  EPIC_HAS_BNC },
94059342fcSbrad 	{ PCI_ID_CODE(PCI_VENDOR_SMC, 0xa016), /* SMC9432FTX */
95059342fcSbrad 	  EPIC_HAS_MII_FIBER | EPIC_DUPLEXLED_ON_694 },
96059342fcSbrad 	{ 0xffffffff,
97059342fcSbrad 	  0 }
98059342fcSbrad };
99059342fcSbrad 
100059342fcSbrad static const struct epic_pci_subsys_info *
epic_pci_subsys_lookup(const struct pci_attach_args * pa)101059342fcSbrad epic_pci_subsys_lookup(const struct pci_attach_args *pa)
102059342fcSbrad {
103059342fcSbrad 	pci_chipset_tag_t pc = pa->pa_pc;
104059342fcSbrad 	pcireg_t reg;
105059342fcSbrad 	const struct epic_pci_subsys_info *esp;
106059342fcSbrad 
107059342fcSbrad 	reg = pci_conf_read(pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
108059342fcSbrad 
109059342fcSbrad 	for (esp = epic_pci_subsys_info; esp->subsysid != 0xffffffff; esp++)
110059342fcSbrad 		if (esp->subsysid == reg)
111059342fcSbrad 			return (esp);
112059342fcSbrad 
113059342fcSbrad 	return (NULL);
114059342fcSbrad }
115059342fcSbrad 
116d0d7b0d0Sbrad int
epic_pci_match(struct device * parent,void * match,void * aux)117059342fcSbrad epic_pci_match(struct device *parent, void *match, void *aux)
118059342fcSbrad {
1190ff4d878Sbrad 	return (pci_matchbyid((struct pci_attach_args *)aux, epic_pci_devices,
120299fb045Sjasper 	    nitems(epic_pci_devices)));
121059342fcSbrad }
122059342fcSbrad 
123d0d7b0d0Sbrad void
epic_pci_attach(struct device * parent,struct device * self,void * aux)124059342fcSbrad epic_pci_attach(struct device *parent, struct device *self, void *aux)
125059342fcSbrad {
126059342fcSbrad 	struct epic_pci_softc *psc = (struct epic_pci_softc *)self;
127059342fcSbrad 	struct epic_softc *sc = &psc->sc_epic;
128059342fcSbrad 	struct pci_attach_args *pa = aux;
129059342fcSbrad 	pci_chipset_tag_t pc = pa->pa_pc;
130059342fcSbrad 	pci_intr_handle_t ih;
131059342fcSbrad 	const char *intrstr = NULL;
132059342fcSbrad 	const struct epic_pci_subsys_info *esp;
133059342fcSbrad 	bus_space_tag_t iot, memt;
134059342fcSbrad 	bus_space_handle_t ioh, memh;
1350832e3b1Sderaadt 	int ioh_valid, memh_valid;
136059342fcSbrad 
1370832e3b1Sderaadt 	pci_set_powerstate(pc, pa->pa_tag, PCI_PMCSR_STATE_D0);
138059342fcSbrad 
139059342fcSbrad 	/*
140059342fcSbrad 	 * Map the device.
141059342fcSbrad 	 */
142059342fcSbrad 	ioh_valid = (pci_mapreg_map(pa, EPIC_PCI_IOBA,
143059342fcSbrad 	    PCI_MAPREG_TYPE_IO, 0,
144059342fcSbrad 	    &iot, &ioh, NULL, NULL, 0) == 0);
145059342fcSbrad 	memh_valid = (pci_mapreg_map(pa, EPIC_PCI_MMBA,
146059342fcSbrad 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
147059342fcSbrad 	    &memt, &memh, NULL, NULL, 0) == 0);
148059342fcSbrad 
149059342fcSbrad 	if (memh_valid) {
150059342fcSbrad 		sc->sc_st = memt;
151059342fcSbrad 		sc->sc_sh = memh;
152059342fcSbrad 	} else if (ioh_valid) {
153059342fcSbrad 		sc->sc_st = iot;
154059342fcSbrad 		sc->sc_sh = ioh;
155059342fcSbrad 	} else {
156ccb87389Smiod 		printf(": unable to map device registers\n");
157059342fcSbrad 		return;
158059342fcSbrad 	}
159059342fcSbrad 
160059342fcSbrad 	sc->sc_dmat = pa->pa_dmat;
161059342fcSbrad 
162059342fcSbrad 	/*
163059342fcSbrad 	 * Map and establish our interrupt.
164059342fcSbrad 	 */
165059342fcSbrad 	if (pci_intr_map(pa, &ih)) {
166ccb87389Smiod 		printf(": unable to map interrupt\n");
167059342fcSbrad 		return;
168059342fcSbrad 	}
169059342fcSbrad 	intrstr = pci_intr_string(pc, ih);
170059342fcSbrad 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, epic_intr, sc,
171059342fcSbrad 	    self->dv_xname);
172059342fcSbrad 	if (psc->sc_ih == NULL) {
17355e06741Skrw 		printf(": unable to establish interrupt");
174059342fcSbrad 		if (intrstr != NULL)
175059342fcSbrad 			printf(" at %s", intrstr);
176059342fcSbrad 		printf("\n");
177059342fcSbrad 		return;
178059342fcSbrad 	}
179059342fcSbrad 
180059342fcSbrad 	esp = epic_pci_subsys_lookup(pa);
181059342fcSbrad 	if (esp)
182059342fcSbrad 		sc->sc_hwflags = esp->flags;
183059342fcSbrad 
184059342fcSbrad 	/*
185059342fcSbrad 	 * Finish off the attach.
186059342fcSbrad 	 */
187059342fcSbrad 	epic_attach(sc, intrstr);
188059342fcSbrad }
189