1 /* $NetBSD: if_mtd_pci.c,v 1.22 2021/05/08 00:27:02 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Peter Bex <Peter.Bex@student.kun.nl>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * PCI interface for MTD803 cards
34 * Written by Peter Bex (peter.bex@student.kun.nl)
35 */
36
37 /* TODO: Check why in IO space, the MII won't work. Memory mapped works */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: if_mtd_pci.c,v 1.22 2021/05/08 00:27:02 thorpej Exp $");
41
42 #include <sys/param.h>
43 #include <sys/device.h>
44 #include <sys/systm.h>
45 #include <sys/socket.h>
46 #include <net/if.h>
47 #include <net/if_ether.h>
48 #include <net/if_media.h>
49 #include <sys/bus.h>
50 #include <dev/mii/miivar.h>
51 #include <dev/ic/mtd803reg.h>
52 #include <dev/ic/mtd803var.h>
53 #include <dev/pci/pcidevs.h>
54 #include <dev/pci/pcireg.h>
55 #include <dev/pci/pcivar.h>
56
57 #define PCI_IO_MAP_REG PCI_BAR(0)
58 #define PCI_MEM_MAP_REG PCI_BAR(1)
59
60 static const struct device_compatible_entry compat_data[] = {
61 { .id = PCI_ID_CODE(PCI_VENDOR_MYSON, PCI_PRODUCT_MYSON_MTD803) },
62
63 PCI_COMPAT_EOL
64 };
65
66 static int mtd_pci_match(device_t, cfdata_t, void *);
67 static void mtd_pci_attach(device_t, device_t, void *);
68
69 CFATTACH_DECL_NEW(mtd_pci, sizeof(struct mtd_softc), mtd_pci_match,
70 mtd_pci_attach, NULL, NULL);
71
72 static int
mtd_pci_match(device_t parent,cfdata_t match,void * aux)73 mtd_pci_match(device_t parent, cfdata_t match, void *aux)
74 {
75 struct pci_attach_args *pa = aux;
76
77 return pci_compatible_match(pa, compat_data);
78 }
79
80 static void
mtd_pci_attach(device_t parent,device_t self,void * aux)81 mtd_pci_attach(device_t parent, device_t self, void *aux)
82 {
83 struct pci_attach_args * const pa = aux;
84 struct mtd_softc * const sc = device_private(self);
85 pci_intr_handle_t ih;
86 const char *intrstring = NULL;
87 bus_space_tag_t iot, memt;
88 bus_space_handle_t ioh, memh;
89 int io_valid, mem_valid;
90 char intrbuf[PCI_INTRSTR_LEN];
91
92 sc->dev = self;
93 pci_aprint_devinfo(pa, NULL);
94
95 io_valid = (pci_mapreg_map(pa, PCI_IO_MAP_REG, PCI_MAPREG_TYPE_IO,
96 0, &iot, &ioh, NULL, NULL) == 0);
97 mem_valid = (pci_mapreg_map(pa, PCI_MEM_MAP_REG, PCI_MAPREG_TYPE_MEM
98 | PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh,
99 NULL, NULL) == 0);
100
101 if (mem_valid) {
102 sc->bus_tag = memt;
103 sc->bus_handle = memh;
104 } else if (io_valid) {
105 sc->bus_tag = iot;
106 sc->bus_handle = ioh;
107 } else {
108 aprint_error_dev(sc->dev,
109 "could not map memory or i/o space\n");
110 return;
111 }
112 sc->dma_tag = pa->pa_dmat;
113
114 /* Do generic attach. Seems this must be done before setting IRQ */
115 mtd_config(sc);
116
117 if (pci_intr_map(pa, &ih)) {
118 aprint_error_dev(sc->dev, "could not map interrupt\n");
119 return;
120 }
121 intrstring = pci_intr_string(pa->pa_pc, ih, intrbuf, sizeof(intrbuf));
122
123 if (pci_intr_establish_xname(pa->pa_pc, ih, IPL_NET, mtd_irq_h, sc,
124 device_xname(self)) == NULL) {
125 aprint_error_dev(sc->dev, "could not establish interrupt");
126 if (intrstring != NULL)
127 aprint_error(" at %s", intrstring);
128 aprint_error("\n");
129 return;
130 } else {
131 aprint_normal_dev(sc->dev, "using %s for interrupt\n",
132 intrstring ? intrstring : "unknown interrupt");
133 }
134 }
135