1 /* $NetBSD: if_mtd_pci.c,v 1.10 2007/10/19 12:00:46 ad 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.10 2007/10/19 12:00:46 ad 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 <sys/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, 85 void *aux) 86 { 87 struct pci_attach_args *pa = aux; 88 struct mtd_pci_device_id *id; 89 90 for (id = mtd_ids; id->vendor != 0; ++id) { 91 if (PCI_VENDOR(pa->pa_id) == id->vendor && 92 PCI_PRODUCT(pa->pa_id) == id->product) 93 return (1); 94 } 95 return (0); 96 } 97 98 static void 99 mtd_pci_attach(struct device *parent, struct device *self, void *aux) 100 { 101 struct pci_attach_args * const pa = aux; 102 struct mtd_softc * const sc = (void *)self; 103 pci_intr_handle_t ih; 104 const char *intrstring = NULL; 105 bus_space_tag_t iot, memt; 106 bus_space_handle_t ioh, memh; 107 int io_valid, mem_valid; 108 char devinfo[256]; 109 110 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo)); 111 printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class)); 112 113 io_valid = (pci_mapreg_map(pa, PCI_IO_MAP_REG, PCI_MAPREG_TYPE_IO, 114 0, &iot, &ioh, NULL, NULL) == 0); 115 mem_valid = (pci_mapreg_map(pa, PCI_MEM_MAP_REG, PCI_MAPREG_TYPE_MEM 116 | PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, 117 NULL, NULL) == 0); 118 119 if (mem_valid) { 120 sc->bus_tag = memt; 121 sc->bus_handle = memh; 122 } else if (io_valid) { 123 sc->bus_tag = iot; 124 sc->bus_handle = ioh; 125 } else { 126 printf("%s: could not map memory or i/o space\n", 127 sc->dev.dv_xname); 128 return; 129 } 130 sc->dma_tag = pa->pa_dmat; 131 132 /* Do generic attach. Seems this must be done before setting IRQ */ 133 mtd_config(sc); 134 135 if (pci_intr_map(pa, &ih)) { 136 printf("%s: could not map interrupt\n", sc->dev.dv_xname); 137 return; 138 } 139 intrstring = pci_intr_string(pa->pa_pc, ih); 140 141 if (pci_intr_establish(pa->pa_pc, ih, IPL_NET, mtd_irq_h, sc) == NULL) { 142 printf("%s: could not establish interrupt", sc->dev.dv_xname); 143 if (intrstring != NULL) 144 printf(" at %s", intrstring); 145 printf("\n"); 146 return; 147 } else { 148 printf("%s: using %s for interrupt\n", 149 sc->dev.dv_xname, 150 intrstring ? intrstring : "unknown interrupt"); 151 } 152 } 153