if_mtd_pci.c revision 1.3
1/* $NetBSD: if_mtd_pci.c,v 1.3 2003/10/15 06:32:35 simonb 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.3 2003/10/15 06:32:35 simonb 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
67struct mtd_pci_device_id {
68	pci_vendor_id_t		vendor;		/* PCI vendor ID */
69	pci_product_id_t	product;	/* PCI product ID */
70};
71
72static struct mtd_pci_device_id mtd_ids[] = {
73	{ PCI_VENDOR_MYSON, PCI_PRODUCT_MYSON_MTD803 },
74	{ 0, 0 }
75};
76
77int	mtd_pci_match __P((struct device *, struct cfdata *, void *));
78void	mtd_pci_attach __P((struct device *, struct device *, void *));
79
80CFATTACH_DECL(mtd_pci, sizeof(struct mtd_softc), mtd_pci_match, mtd_pci_attach,
81    NULL, NULL);
82
83int
84mtd_pci_match(parent, match, aux)
85	struct device *parent;
86	struct cfdata *match;
87	void *aux;
88{
89	struct pci_attach_args *pa = aux;
90	struct mtd_pci_device_id *id;
91
92	for (id = mtd_ids; id->vendor != 0; ++id) {
93		if (PCI_VENDOR(pa->pa_id) == id->vendor &&
94		    PCI_PRODUCT(pa->pa_id) == id->product)
95			return (1);
96	}
97	return (0);
98}
99
100void
101mtd_pci_attach(parent, self, aux)
102	struct device *parent;
103	struct device *self;
104	void *aux;
105{
106	struct pci_attach_args * const pa = aux;
107	struct mtd_softc * const sc = (void *)self;
108	pci_intr_handle_t ih;
109	const char *intrstring = NULL;
110	bus_space_tag_t iot, memt;
111	bus_space_handle_t ioh, memh;
112	int io_valid, mem_valid;
113	char devinfo[256];
114
115	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
116	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
117
118	io_valid = (pci_mapreg_map(pa, PCI_IO_MAP_REG, PCI_MAPREG_TYPE_IO,
119			0, &iot, &ioh, NULL, NULL) == 0);
120	mem_valid = (pci_mapreg_map(pa, PCI_MEM_MAP_REG, PCI_MAPREG_TYPE_MEM
121			| PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh,
122			NULL, NULL) == 0);
123
124	if (mem_valid) {
125		sc->bus_tag = memt;
126		sc->bus_handle = memh;
127	} else if (io_valid) {
128		sc->bus_tag = iot;
129		sc->bus_handle = ioh;
130	} else {
131		printf("%s: could not map memory or i/o space\n",
132			sc->dev.dv_xname);
133		return;
134	}
135	sc->dma_tag = pa->pa_dmat;
136
137	/* Do generic attach. Seems this must be done before setting IRQ */
138	mtd_config(sc);
139
140	if (pci_intr_map(pa, &ih)) {
141		printf("%s: could not map interrupt\n", sc->dev.dv_xname);
142		return;
143	}
144	intrstring = pci_intr_string(pa->pa_pc, ih);
145
146	if (pci_intr_establish(pa->pa_pc, ih, IPL_NET, mtd_irq_h, sc) == NULL) {
147		printf("%s: could not establish interrupt", sc->dev.dv_xname);
148		if (intrstring != NULL)
149			printf(" at %s", intrstring);
150		printf("\n");
151		return;
152	} else {
153		printf("%s: using %s for interrupt\n",
154			sc->dev.dv_xname,
155			intrstring ? intrstring : "unknown interrupt");
156	}
157}
158