xref: /netbsd/sys/dev/pci/if_mtd_pci.c (revision 6550d01e)
1 /* $NetBSD: if_mtd_pci.c,v 1.15 2009/11/26 15:17:09 njoly 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.15 2009/11/26 15:17:09 njoly 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		0x10
58 #define PCI_MEM_MAP_REG		0x14
59 
60 struct mtd_pci_device_id {
61 	pci_vendor_id_t		vendor;		/* PCI vendor ID */
62 	pci_product_id_t	product;	/* PCI product ID */
63 };
64 
65 static struct mtd_pci_device_id mtd_ids[] = {
66 	{ PCI_VENDOR_MYSON, PCI_PRODUCT_MYSON_MTD803 },
67 	{ 0, 0 }
68 };
69 
70 static int	mtd_pci_match(device_t, cfdata_t, void *);
71 static void	mtd_pci_attach(device_t, device_t, void *);
72 
73 CFATTACH_DECL(mtd_pci, sizeof(struct mtd_softc), mtd_pci_match, mtd_pci_attach,
74     NULL, NULL);
75 
76 static int
77 mtd_pci_match(device_t parent, cfdata_t match, void *aux)
78 {
79 	struct pci_attach_args *pa = aux;
80 	struct mtd_pci_device_id *id;
81 
82 	for (id = mtd_ids; id->vendor != 0; ++id) {
83 		if (PCI_VENDOR(pa->pa_id) == id->vendor &&
84 		    PCI_PRODUCT(pa->pa_id) == id->product)
85 			return (1);
86 	}
87 	return (0);
88 }
89 
90 static void
91 mtd_pci_attach(device_t parent, device_t self, void *aux)
92 {
93 	struct pci_attach_args * const pa = aux;
94 	struct mtd_softc * const sc = device_private(self);
95 	pci_intr_handle_t ih;
96 	const char *intrstring = NULL;
97 	bus_space_tag_t iot, memt;
98 	bus_space_handle_t ioh, memh;
99 	int io_valid, mem_valid;
100 	char devinfo[256];
101 
102 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
103 	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
104 
105 	io_valid = (pci_mapreg_map(pa, PCI_IO_MAP_REG, PCI_MAPREG_TYPE_IO,
106 			0, &iot, &ioh, NULL, NULL) == 0);
107 	mem_valid = (pci_mapreg_map(pa, PCI_MEM_MAP_REG, PCI_MAPREG_TYPE_MEM
108 			| PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh,
109 			NULL, NULL) == 0);
110 
111 	if (mem_valid) {
112 		sc->bus_tag = memt;
113 		sc->bus_handle = memh;
114 	} else if (io_valid) {
115 		sc->bus_tag = iot;
116 		sc->bus_handle = ioh;
117 	} else {
118 		aprint_error_dev(&sc->dev, "could not map memory or i/o space\n");
119 		return;
120 	}
121 	sc->dma_tag = pa->pa_dmat;
122 
123 	/* Do generic attach. Seems this must be done before setting IRQ */
124 	mtd_config(sc);
125 
126 	if (pci_intr_map(pa, &ih)) {
127 		aprint_error_dev(&sc->dev, "could not map interrupt\n");
128 		return;
129 	}
130 	intrstring = pci_intr_string(pa->pa_pc, ih);
131 
132 	if (pci_intr_establish(pa->pa_pc, ih, IPL_NET, mtd_irq_h, sc) == NULL) {
133 		aprint_error_dev(&sc->dev, "could not establish interrupt");
134 		if (intrstring != NULL)
135 			aprint_error(" at %s", intrstring);
136 		aprint_error("\n");
137 		return;
138 	} else {
139 		aprint_normal_dev(&sc->dev, "using %s for interrupt\n",
140 			intrstring ? intrstring : "unknown interrupt");
141 	}
142 }
143