if_mtd_pci.c revision 1.2
1/*	$OpenBSD: if_mtd_pci.c,v 1.2 2003/08/19 04:45:04 mickey Exp $	*/
2
3/*
4 * Copyright (c) 2003 Oleg Safiullin
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice unmodified, this list of conditions, and the following
12 *    disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30
31#include <sys/param.h>
32#include <sys/device.h>
33#include <sys/systm.h>
34#include <sys/socket.h>
35
36#include <net/if.h>
37#include <net/if_media.h>
38
39#ifdef INET
40#include <netinet/in.h>
41#include <netinet/if_ether.h>
42#endif
43
44#include <machine/bus.h>
45
46#include <dev/mii/miivar.h>
47
48#include <dev/pci/pcidevs.h>
49#include <dev/pci/pcireg.h>
50#include <dev/pci/pcivar.h>
51
52#include <dev/ic/mtd803reg.h>
53#include <dev/ic/mtd803var.h>
54
55static int mtd_pci_match(struct device *, void *, void *);
56static void mtd_pci_attach(struct device *, struct device *, void *);
57
58struct cfattach mtd_pci_ca = {
59	sizeof(struct mtd_softc), mtd_pci_match, mtd_pci_attach
60};
61
62const static struct pci_matchid mtd_pci_devices[] = {
63	{ PCI_VENDOR_MYSON, PCI_PRODUCT_MYSON_MTD803 },
64};
65
66static int
67mtd_pci_match(struct device *parent, void *match, void *aux)
68{
69	return (pci_matchbyid((struct pci_attach_args *)aux, mtd_pci_devices,
70	    sizeof(mtd_pci_devices) / sizeof(mtd_pci_devices[0])));
71}
72
73static void
74mtd_pci_attach(struct device *parent, struct device *self, void *aux)
75{
76	struct mtd_softc *sc = (void *)self;
77	struct pci_attach_args *pa = aux;
78	pci_intr_handle_t ih;
79	const char *intrstr = NULL;
80	bus_size_t iosize;
81	u_int32_t command;
82
83	command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
84
85#ifdef MTD_USE_MEMIO
86	if (pci_mapreg_map(pa, MTD_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
87	    &sc->bus_tag, &sc->bus_handle, NULL, &iosize, 0)) {
88		printf(": can't map mem space\n");
89		return;
90	}
91#else	/* !MTD_USE_MEMIO */
92	if (pci_mapreg_map(pa, MTD_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
93	    &sc->bus_tag, &sc->bus_handle, NULL, &iosize, 0)) {
94		printf(": can't map io space\n");
95		return;
96	}
97#endif	/* MTD_USE_MEMIO */
98
99	/*
100	 * Allocate our interrupt.
101	 */
102	if (pci_intr_map(pa, &ih)) {
103		printf(": couldn't map interrupt\n");
104		bus_space_unmap(sc->bus_tag, sc->bus_handle, iosize);
105		return;
106	}
107
108	intrstr = pci_intr_string(pa->pa_pc, ih);
109	if (pci_intr_establish(pa->pa_pc, ih, IPL_NET, mtd_irq_h, sc,
110	    self->dv_xname) == NULL) {
111		printf(": couldn't establish interrupt");
112		if (intrstr != NULL)
113			printf(" at %s", intrstr);
114		printf("\n");
115		bus_space_unmap(sc->bus_tag, sc->bus_handle, iosize);
116		return;
117	}
118	printf(": %s", intrstr);
119
120	sc->dma_tag = pa->pa_dmat;
121	mtd_config(sc);
122}
123