xref: /netbsd-src/sys/dev/pci/if_mtd_pci.c (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1 /* $NetBSD: if_mtd_pci.c,v 1.1 2002/11/07 21:57:01 martin 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * PCI interface for MTD803 cards
41  * Written by Peter Bex (peter.bex@student.kun.nl)
42  */
43 
44 /* TODO: Check why in IO space, the MII won't work. Memory mapped works */
45 
46 #include <sys/param.h>
47 #include <sys/device.h>
48 #include <sys/systm.h>
49 #include <sys/socket.h>
50 #include <net/if.h>
51 #include <net/if_ether.h>
52 #include <net/if_media.h>
53 #include <machine/bus.h>
54 #include <dev/mii/miivar.h>
55 #include <dev/ic/mtd803reg.h>
56 #include <dev/ic/mtd803var.h>
57 #include <dev/pci/pcidevs.h>
58 #include <dev/pci/pcireg.h>
59 #include <dev/pci/pcivar.h>
60 
61 #define PCI_IO_MAP_REG		0x10
62 #define PCI_MEM_MAP_REG		0x14
63 
64 struct mtd_pci_device_id {
65 	pci_vendor_id_t		vendor;		/* PCI vendor ID */
66 	pci_product_id_t	product;	/* PCI product ID */
67 };
68 
69 static struct mtd_pci_device_id mtd_ids[] = {
70 	{ PCI_VENDOR_MYSON, PCI_PRODUCT_MYSON_MTD803 },
71 	{ 0, 0 }
72 };
73 
74 int	mtd_pci_match __P((struct device *, struct cfdata *, void *));
75 void	mtd_pci_attach __P((struct device *, struct device *, void *));
76 
77 CFATTACH_DECL(mtd_pci, sizeof(struct mtd_softc), mtd_pci_match, mtd_pci_attach,
78     NULL, NULL);
79 
80 int
81 mtd_pci_match(parent, match, aux)
82 	struct device *parent;
83 	struct cfdata *match;
84 	void *aux;
85 {
86 	struct pci_attach_args *pa = aux;
87 	struct mtd_pci_device_id *id;
88 
89 	for (id = mtd_ids; id->vendor != 0; ++id) {
90 		if (PCI_VENDOR(pa->pa_id) == id->vendor &&
91 		    PCI_PRODUCT(pa->pa_id) == id->product)
92 			return (1);
93 	}
94 	return (0);
95 }
96 
97 void
98 mtd_pci_attach(parent, self, aux)
99 	struct device *parent;
100 	struct device *self;
101 	void *aux;
102 {
103 	struct pci_attach_args * const pa = aux;
104 	struct mtd_softc * const sc = (void *)self;
105 	pci_intr_handle_t ih;
106 	const char *intrstring = NULL;
107 	bus_space_tag_t iot, memt;
108 	bus_space_handle_t ioh, memh;
109 	int io_valid, mem_valid;
110 	char devinfo[256];
111 
112 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
113 	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
114 
115 	io_valid = (pci_mapreg_map(pa, PCI_IO_MAP_REG, PCI_MAPREG_TYPE_IO,
116 			0, &memt, &memh, NULL, NULL) == 0);
117 	mem_valid = (pci_mapreg_map(pa, PCI_MEM_MAP_REG, PCI_MAPREG_TYPE_MEM
118 			| PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh,
119 			NULL, NULL) == 0);
120 
121 	if (mem_valid) {
122 		sc->bus_tag = memt;
123 		sc->bus_handle = memh;
124 	} else if (io_valid) {
125 		sc->bus_tag = iot;
126 		sc->bus_handle = ioh;
127 	} else {
128 		printf("%s: could not map memory or i/o space\n",
129 			sc->dev.dv_xname);
130 		return;
131 	}
132 	sc->dma_tag = pa->pa_dmat;
133 
134 	/* Do generic attach. Seems this must be done before setting IRQ */
135 	mtd_config(sc);
136 
137 	if (pci_intr_map(pa, &ih)) {
138 		printf("%s: could not map interrupt\n", sc->dev.dv_xname);
139 		return;
140 	}
141 	intrstring = pci_intr_string(pa->pa_pc, ih);
142 
143 	if (pci_intr_establish(pa->pa_pc, ih, IPL_NET, mtd_irq_h, sc) == NULL) {
144 		printf("%s: could not establish interrupt", sc->dev.dv_xname);
145 		if (intrstring != NULL)
146 			printf(" at %s", intrstring);
147 		printf("\n");
148 		return;
149 	} else {
150 		printf("%s: using %s for interrupt\n",
151 			sc->dev.dv_xname,
152 			intrstring ? intrstring : "unknown interrupt");
153 	}
154 }
155