xref: /openbsd-src/sys/dev/pci/if_mtd_pci.c (revision 0f9891f1fafd8f53a63c41edb56ce51e2589b910)
1 /*	$OpenBSD: if_mtd_pci.c,v 1.14 2024/05/24 06:02:56 jsg Exp $	*/
2 
3 /*
4  * Copyright (c) 2003 Oleg Safiullin <form@pdp11.org.ru>
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 unmodified, this list of conditions, and the following
12  *    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  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 
31 #include <sys/param.h>
32 #include <sys/device.h>
33 #include <sys/systm.h>
34 
35 #include <net/if.h>
36 #include <net/if_media.h>
37 
38 #include <netinet/in.h>
39 #include <netinet/if_ether.h>
40 
41 #include <machine/bus.h>
42 
43 #include <dev/mii/miivar.h>
44 
45 #include <dev/pci/pcidevs.h>
46 #include <dev/pci/pcireg.h>
47 #include <dev/pci/pcivar.h>
48 
49 #include <dev/ic/mtd8xxreg.h>
50 #include <dev/ic/mtd8xxvar.h>
51 
52 static int mtd_pci_match(struct device *, void *, void *);
53 static void mtd_pci_attach(struct device *, struct device *, void *);
54 
55 
56 const struct cfattach mtd_pci_ca = {
57 	sizeof(struct mtd_softc), mtd_pci_match, mtd_pci_attach
58 };
59 
60 static const struct pci_matchid mtd_pci_devices[] = {
61 	{ PCI_VENDOR_MYSON, PCI_PRODUCT_MYSON_MTD800 },
62 	{ PCI_VENDOR_MYSON, PCI_PRODUCT_MYSON_MTD803 },
63 	{ PCI_VENDOR_MYSON, PCI_PRODUCT_MYSON_MTD891 },
64 };
65 
66 
67 static int
mtd_pci_match(struct device * parent,void * match,void * aux)68 mtd_pci_match(struct device *parent, void *match, void *aux)
69 {
70 	return (pci_matchbyid((struct pci_attach_args *)aux, mtd_pci_devices,
71 	    sizeof(mtd_pci_devices) / sizeof(mtd_pci_devices[0])));
72 }
73 
74 
75 static void
mtd_pci_attach(struct device * parent,struct device * self,void * aux)76 mtd_pci_attach(struct device *parent, struct device *self, void *aux)
77 {
78 	struct mtd_softc *sc = (void *)self;
79 	struct pci_attach_args *pa = aux;
80 	pci_intr_handle_t ih;
81 	const char *intrstr = NULL;
82 	u_int32_t command;
83 	bus_size_t iosize;
84 
85 	sc->sc_devid = PCI_PRODUCT(pa->pa_id);
86 	command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
87 	if (sc->sc_devid == PCI_PRODUCT_MYSON_MTD800 &&
88 	    pci_conf_read(pa->pa_pc, pa->pa_tag, MTD_PCI_LOIO) & 0x300) {
89 		pa->pa_flags &= ~PCI_FLAGS_IO_ENABLED;
90 		command &= ~PCI_COMMAND_IO_ENABLE;
91 	}
92 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
93 
94 	command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
95 	if (command & PCI_COMMAND_MEM_ENABLE) {
96 		if (pci_mapreg_map(pa, MTD_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
97 		    &sc->sc_bust, &sc->sc_bush, NULL, &iosize, 0)) {
98 			printf(": can't map mem space\n");
99 			return;
100 		}
101 	} else {
102 		if (pci_mapreg_map(pa, MTD_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
103 		    &sc->sc_bust, &sc->sc_bush, NULL, &iosize, 0)) {
104 			printf(": can't map io space\n");
105 			return;
106 		}
107 	}
108 
109 	/*
110 	 * Allocate our interrupt.
111 	 */
112 	if (pci_intr_map(pa, &ih)) {
113 		printf(": couldn't map interrupt\n");
114 		bus_space_unmap(sc->sc_bust, sc->sc_bush, iosize);
115 		return;
116 	}
117 
118 	intrstr = pci_intr_string(pa->pa_pc, ih);
119 	if (pci_intr_establish(pa->pa_pc, ih, IPL_NET, mtd_intr, sc,
120 	    self->dv_xname) == NULL) {
121 		printf(": couldn't establish interrupt");
122 		if (intrstr != NULL)
123 			printf(" at %s", intrstr);
124 		printf("\n");
125 		bus_space_unmap(sc->sc_bust, sc->sc_bush, iosize);
126 		return;
127 	}
128 	printf(": %s", intrstr);
129 
130 	sc->sc_dmat = pa->pa_dmat;
131 	mtd_attach(sc);
132 }
133