| 1 | /*- |
| 2 | * Copyright (c) 1999 - 2001 John Hay. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. Neither the name of the author nor the names of any co-contributors |
| 14 | * may be used to endorse or promote products derived from this software |
| 15 | * without specific prior written permission. |
| 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 | * $FreeBSD: src/sys/dev/ar/if_ar_pci.c,v 1.11 2004/05/30 20:08:26 phk Exp $ |
| 30 | * $DragonFly: src/sys/dev/netif/ar/if_ar_pci.c,v 1.6 2006/10/25 20:55:55 dillon Exp $ |
| 31 | */ |
| 32 | |
| 33 | #include <sys/param.h> |
| 34 | #include <sys/systm.h> |
| 35 | #include <sys/kernel.h> |
| 36 | #include <sys/malloc.h> |
| 37 | #include <sys/module.h> |
| 38 | #include <sys/bus.h> |
| 39 | #include <sys/rman.h> |
| 40 | |
| 41 | #include <bus/pci/pcidevs.h> |
| 42 | #include <bus/pci/pcireg.h> |
| 43 | #include <bus/pci/pcivar.h> |
| 44 | |
| 45 | #include <dev/netif/ic_layer/hd64570.h> |
| 46 | #include <dev/netif/ar/if_arregs.h> |
| 47 | |
| 48 | #ifdef TRACE |
| 49 | #define TRC(x) x |
| 50 | #else |
| 51 | #define TRC(x) |
| 52 | #endif |
| 53 | |
| 54 | #define TRCL(x) x |
| 55 | |
| 56 | static struct ar_type { |
| 57 | uint16_t ar_vid; |
| 58 | uint16_t ar_did; |
| 59 | const char *ar_name; |
| 60 | const char *ar_errmsg; |
| 61 | } ar_devs[] = { |
| 62 | { PCI_VENDOR_DIGI, PCI_PRODUCT_DIGI_SYNC570I_2P, |
| 63 | "Digi SYNC/570i-PCI 2 port", |
| 64 | NULL }, |
| 65 | { PCI_VENDOR_DIGI, PCI_PRODUCT_DIGI_SYNC570I_2PB1, |
| 66 | "Digi SYNC/570i-PCI 2 port (mapped below 1M)", |
| 67 | "Please change the jumper to select linear mode." }, |
| 68 | { PCI_VENDOR_DIGI, PCI_PRODUCT_DIGI_SYNC570I_4P, |
| 69 | "Digi SYNC/570i-PCI 4 port", |
| 70 | NULL }, |
| 71 | { PCI_VENDOR_DIGI, PCI_PRODUCT_DIGI_SYNC570I_4PB1, |
| 72 | "Digi SYNC/570i-PCI 4 port (mapped below 1M)", |
| 73 | "Please change the jumper to select linear mode." }, |
| 74 | { 0, 0, NULL } |
| 75 | }; |
| 76 | |
| 77 | static int ar_pci_probe(device_t); |
| 78 | static int ar_pci_attach(device_t); |
| 79 | |
| 80 | static device_method_t ar_pci_methods[] = { |
| 81 | /* Device interface */ |
| 82 | DEVMETHOD(device_probe, ar_pci_probe), |
| 83 | DEVMETHOD(device_attach, ar_pci_attach), |
| 84 | DEVMETHOD(device_detach, ar_detach), |
| 85 | { 0, 0 } |
| 86 | }; |
| 87 | |
| 88 | static driver_t ar_pci_driver = { |
| 89 | "ar", |
| 90 | ar_pci_methods, |
| 91 | sizeof(struct ar_hardc), |
| 92 | }; |
| 93 | |
| 94 | DRIVER_MODULE(if_ar, pci, ar_pci_driver, ar_devclass, 0, 0); |
| 95 | |
| 96 | static int |
| 97 | ar_pci_probe(device_t device) |
| 98 | { |
| 99 | struct ar_type *t; |
| 100 | uint16_t product = pci_get_device(device); |
| 101 | uint16_t vendor = pci_get_vendor(device); |
| 102 | |
| 103 | for (t = ar_devs; t->ar_name != NULL; t++) { |
| 104 | if (vendor == t->ar_vid && product == t->ar_did) { |
| 105 | if (t->ar_errmsg != NULL) { |
| 106 | printf("%s\n%s\n", t->ar_name, t->ar_errmsg); |
| 107 | return(ENXIO); |
| 108 | } else { |
| 109 | device_set_desc(device, t->ar_name); |
| 110 | return(0); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | return (ENXIO); |
| 115 | } |
| 116 | |
| 117 | static int |
| 118 | ar_pci_attach(device_t device) |
| 119 | { |
| 120 | int error; |
| 121 | u_int i, tmp; |
| 122 | struct ar_hardc *hc; |
| 123 | |
| 124 | hc = (struct ar_hardc *)device_get_softc(device); |
| 125 | bzero(hc, sizeof(struct ar_hardc)); |
| 126 | |
| 127 | error = ar_allocate_plx_memory(device, 0x10, 1); |
| 128 | if(error) |
| 129 | goto errexit; |
| 130 | |
| 131 | error = ar_allocate_memory(device, 0x18, 1); |
| 132 | if(error) |
| 133 | goto errexit; |
| 134 | |
| 135 | error = ar_allocate_irq(device, 0, 1); |
| 136 | if(error) |
| 137 | goto errexit; |
| 138 | |
| 139 | hc->mem_start = rman_get_virtual(hc->res_memory); |
| 140 | |
| 141 | hc->cunit = device_get_unit(device); |
| 142 | hc->sca[0] = (sca_regs *)(hc->mem_start + AR_PCI_SCA_1_OFFSET); |
| 143 | hc->sca[1] = (sca_regs *)(hc->mem_start + AR_PCI_SCA_2_OFFSET); |
| 144 | hc->orbase = (u_char *)(hc->mem_start + AR_PCI_ORBASE_OFFSET); |
| 145 | |
| 146 | tmp = hc->orbase[AR_BMI * 4]; |
| 147 | hc->bustype = tmp & AR_BUS_MSK; |
| 148 | hc->memsize = (tmp & AR_MEM_MSK) >> AR_MEM_SHFT; |
| 149 | hc->memsize = 1 << hc->memsize; |
| 150 | hc->memsize <<= 16; |
| 151 | hc->interface[0] = (tmp & AR_IFACE_MSK); |
| 152 | tmp = hc->orbase[AR_REV * 4]; |
| 153 | hc->revision = tmp & AR_REV_MSK; |
| 154 | hc->winsize = (1 << ((tmp & AR_WSIZ_MSK) >> AR_WSIZ_SHFT)) * 16 * 1024; |
| 155 | hc->mem_end = (caddr_t)(hc->mem_start + hc->winsize); |
| 156 | hc->winmsk = hc->winsize - 1; |
| 157 | hc->numports = hc->orbase[AR_PNUM * 4]; |
| 158 | hc->handshake = hc->orbase[AR_HNDSH * 4]; |
| 159 | |
| 160 | for(i = 1; i < hc->numports; i++) |
| 161 | hc->interface[i] = hc->interface[0]; |
| 162 | |
| 163 | TRC(printf("arp%d: bus %x, rev %d, memstart %p, winsize %d, " |
| 164 | "winmsk %x, interface %x\n", |
| 165 | unit, hc->bustype, hc->revision, hc->mem_start, hc->winsize, |
| 166 | hc->winmsk, hc->interface[0])); |
| 167 | |
| 168 | ar_attach(device); |
| 169 | |
| 170 | /* Magic to enable the card to generate interrupts. */ |
| 171 | bus_space_write_1(rman_get_bustag(hc->res_plx_memory), |
| 172 | rman_get_bushandle(hc->res_plx_memory), 0x69, 0x09); |
| 173 | |
| 174 | return (0); |
| 175 | |
| 176 | errexit: |
| 177 | ar_deallocate_resources(device); |
| 178 | return (ENXIO); |
| 179 | } |
| 180 | |