xref: /netbsd-src/sys/dev/pci/if_mtd_pci.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /* $NetBSD: if_mtd_pci.c,v 1.5 2004/08/21 23:48:33 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  * 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/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: if_mtd_pci.c,v 1.5 2004/08/21 23:48:33 thorpej Exp $");
48 
49 #include <sys/param.h>
50 #include <sys/device.h>
51 #include <sys/systm.h>
52 #include <sys/socket.h>
53 #include <net/if.h>
54 #include <net/if_ether.h>
55 #include <net/if_media.h>
56 #include <machine/bus.h>
57 #include <dev/mii/miivar.h>
58 #include <dev/ic/mtd803reg.h>
59 #include <dev/ic/mtd803var.h>
60 #include <dev/pci/pcidevs.h>
61 #include <dev/pci/pcireg.h>
62 #include <dev/pci/pcivar.h>
63 
64 #define PCI_IO_MAP_REG		0x10
65 #define PCI_MEM_MAP_REG		0x14
66 
67 struct mtd_pci_device_id {
68 	pci_vendor_id_t		vendor;		/* PCI vendor ID */
69 	pci_product_id_t	product;	/* PCI product ID */
70 };
71 
72 static struct mtd_pci_device_id mtd_ids[] = {
73 	{ PCI_VENDOR_MYSON, PCI_PRODUCT_MYSON_MTD803 },
74 	{ 0, 0 }
75 };
76 
77 static int	mtd_pci_match(struct device *, struct cfdata *, void *);
78 static void	mtd_pci_attach(struct device *, struct device *, void *);
79 
80 CFATTACH_DECL(mtd_pci, sizeof(struct mtd_softc), mtd_pci_match, mtd_pci_attach,
81     NULL, NULL);
82 
83 static int
84 mtd_pci_match(struct device *parent, struct cfdata *match, 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 static void
98 mtd_pci_attach(struct device *parent, struct device *self, void *aux)
99 {
100 	struct pci_attach_args * const pa = aux;
101 	struct mtd_softc * const sc = (void *)self;
102 	pci_intr_handle_t ih;
103 	const char *intrstring = NULL;
104 	bus_space_tag_t iot, memt;
105 	bus_space_handle_t ioh, memh;
106 	int io_valid, mem_valid;
107 	char devinfo[256];
108 
109 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
110 	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
111 
112 	io_valid = (pci_mapreg_map(pa, PCI_IO_MAP_REG, PCI_MAPREG_TYPE_IO,
113 			0, &iot, &ioh, NULL, NULL) == 0);
114 	mem_valid = (pci_mapreg_map(pa, PCI_MEM_MAP_REG, PCI_MAPREG_TYPE_MEM
115 			| PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh,
116 			NULL, NULL) == 0);
117 
118 	if (mem_valid) {
119 		sc->bus_tag = memt;
120 		sc->bus_handle = memh;
121 	} else if (io_valid) {
122 		sc->bus_tag = iot;
123 		sc->bus_handle = ioh;
124 	} else {
125 		printf("%s: could not map memory or i/o space\n",
126 			sc->dev.dv_xname);
127 		return;
128 	}
129 	sc->dma_tag = pa->pa_dmat;
130 
131 	/* Do generic attach. Seems this must be done before setting IRQ */
132 	mtd_config(sc);
133 
134 	if (pci_intr_map(pa, &ih)) {
135 		printf("%s: could not map interrupt\n", sc->dev.dv_xname);
136 		return;
137 	}
138 	intrstring = pci_intr_string(pa->pa_pc, ih);
139 
140 	if (pci_intr_establish(pa->pa_pc, ih, IPL_NET, mtd_irq_h, sc) == NULL) {
141 		printf("%s: could not establish interrupt", sc->dev.dv_xname);
142 		if (intrstring != NULL)
143 			printf(" at %s", intrstring);
144 		printf("\n");
145 		return;
146 	} else {
147 		printf("%s: using %s for interrupt\n",
148 			sc->dev.dv_xname,
149 			intrstring ? intrstring : "unknown interrupt");
150 	}
151 }
152