1 /* $NetBSD: i82365_pci.c,v 1.2 1997/10/16 23:23:14 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1997 Marc Horowitz. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Marc Horowitz. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/types.h> 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/device.h> 36 37 #include <dev/ic/i82365reg.h> 38 #include <dev/ic/i82365var.h> 39 40 #include <dev/pci/pcivar.h> 41 #include <dev/pci/pcireg.h> 42 #include <dev/pci/pcidevs.h> 43 44 /* 45 * PCI constants. 46 * XXX These should be in a common file! 47 */ 48 #define PCI_CBIO 0x10 /* Configuration Base IO Address */ 49 50 #ifdef __BROKEN_INDIRECT_CONFIG 51 int pcic_pci_match __P((struct device *, void *, void *)); 52 #else 53 int pcic_pci_match __P((struct device *, struct cfdata *, void *)); 54 #endif 55 void pcic_pci_attach __P((struct device *, struct device *, void *)); 56 57 void *pcic_pci_chip_intr_establish __P((pcmcia_chipset_handle_t, 58 struct pcmcia_function *, int, int (*) (void *), void *)); 59 void pcic_pci_chip_intr_disestablish __P((pcmcia_chipset_handle_t, void *)); 60 61 struct cfattach pcic_pci_ca = { 62 sizeof(struct pcic_softc), pcic_pci_match, pcic_pci_attach 63 }; 64 65 static struct pcmcia_chip_functions pcic_pci_functions = { 66 pcic_chip_mem_alloc, 67 pcic_chip_mem_free, 68 pcic_chip_mem_map, 69 pcic_chip_mem_unmap, 70 71 pcic_chip_io_alloc, 72 pcic_chip_io_free, 73 pcic_chip_io_map, 74 pcic_chip_io_unmap, 75 76 pcic_pci_chip_intr_establish, 77 pcic_pci_chip_intr_disestablish, 78 79 pcic_chip_socket_enable, 80 pcic_chip_socket_disable, 81 }; 82 83 int 84 pcic_pci_match(parent, match, aux) 85 struct device *parent; 86 #ifdef __BROKEN_INDIRECT_CONFIG 87 void *match; 88 #else 89 struct cfdata *match; 90 #endif 91 void *aux; 92 { 93 struct pci_attach_args *pa = (struct pci_attach_args *) aux; 94 95 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_CIRRUS) 96 return (0); 97 98 switch (PCI_PRODUCT(pa->pa_id)) { 99 case PCI_PRODUCT_CIRRUS_CL_GD6729: 100 break; 101 default: 102 return (0); 103 } 104 105 return (1); 106 } 107 108 void 109 pcic_pci_attach(parent, self, aux) 110 struct device *parent, *self; 111 void *aux; 112 { 113 struct pcic_softc *sc = (void *) self; 114 struct pci_attach_args *pa = aux; 115 pci_chipset_tag_t pc = pa->pa_pc; 116 bus_space_tag_t memt = pa->pa_memt; 117 bus_space_handle_t memh; 118 pci_intr_handle_t ih; 119 char *model; 120 const char *intrstr = NULL; 121 122 if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, 123 &sc->iot, &sc->ioh, NULL, NULL)) { 124 printf(": can't map i/o space\n"); 125 return; 126 } 127 128 /* 129 * XXX need some memory for mapping pcmcia cards into. Ideally, this 130 * would be completely dynamic. Practically this doesn't work, 131 * because the extent mapper doesn't know about all the devices all 132 * the time. With ISA we could finesse the issue by specifying the 133 * memory region in the config line. We can't do that here, so we 134 * cheat for now. Jason Thorpe, you are my Savior, come up with a fix 135 * :-) 136 */ 137 138 /* Map mem space. */ 139 if (bus_space_map(memt, 0xd0000, 0x4000, 0, &memh)) 140 panic("pcic_isa_attach: can't map i/o space"); 141 142 sc->membase = 0xd0000; 143 sc->subregionmask = (1 << (0x4000 / PCIC_MEM_PAGESIZE)) - 1; 144 145 /* same deal for io allocation */ 146 147 sc->iobase = 0x400; 148 sc->iosize = 0xbff; 149 150 /* end XXX */ 151 152 sc->intr_est = pc; 153 sc->pct = (pcmcia_chipset_tag_t) & pcic_pci_functions; 154 155 sc->memt = memt; 156 sc->memh = memh; 157 158 switch (PCI_PRODUCT(pa->pa_id)) { 159 case PCI_PRODUCT_CIRRUS_CL_GD6729: 160 model = "Cirrus Logic GD6729 PCMCIA controller"; 161 break; 162 default: 163 model = "Model unknown"; 164 break; 165 } 166 167 printf(": %s\n", model); 168 169 /* Enable the card. */ 170 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, 171 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) | 172 PCI_COMMAND_MASTER_ENABLE); 173 174 pcic_attach(sc); 175 176 /* Map and establish the interrupt. */ 177 if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin, 178 pa->pa_intrline, &ih)) { 179 printf("%s: couldn't map interrupt\n", sc->dev.dv_xname); 180 return; 181 } 182 intrstr = pci_intr_string(pc, ih); 183 sc->ih = pci_intr_establish(pc, ih, IPL_NET, pcic_intr, sc); 184 if (sc->ih == NULL) { 185 printf("%s: couldn't establish interrupt", 186 sc->dev.dv_xname); 187 if (intrstr != NULL) 188 printf(" at %s", intrstr); 189 printf("\n"); 190 return; 191 } 192 printf("%s: interrupting at %s\n", sc->dev.dv_xname, intrstr); 193 194 pcic_attach_sockets(sc); 195 } 196 197 /* 198 * XXX This is almost totally wrong! We need to map to PCI interupts, 199 * XXX which themselves map to somthing else. 200 */ 201 202 #include <dev/isa/isareg.h> 203 #include <dev/isa/isavar.h> 204 205 void * 206 pcic_pci_chip_intr_establish(pch, pf, ipl, fct, arg) 207 pcmcia_chipset_handle_t pch; 208 struct pcmcia_function *pf; 209 int ipl; 210 int (*fct) (void *); 211 void *arg; 212 { 213 struct pcic_handle *h = (struct pcic_handle *) pch; 214 int irq; 215 pci_intr_handle_t piht; 216 void *ih; 217 int reg; 218 219 /* 220 * XXX this will work for x86, but is guaranteed to lose elsewhere. 221 * Hopefully Jason can bail me out of this one, too. 222 */ 223 224 isa_intr_alloc(NULL, 0xffff, IST_PULSE, &irq); 225 226 if (pci_intr_map(h->sc->intr_est, pci_make_tag(NULL, 0, 0, 0), 227 1, irq, &piht)) { 228 printf("%s: couldn't map interrupt\n", h->sc->dev.dv_xname); 229 return (NULL); 230 } 231 if ((ih = pci_intr_establish(h->sc->intr_est, piht, ipl, fct, 232 arg)) == NULL) 233 return (NULL); 234 235 reg = pcic_read(h, PCIC_INTR); 236 reg |= PCIC_INTR_ENABLE; 237 reg |= irq; 238 pcic_write(h, PCIC_INTR, reg); 239 240 printf("%s: card irq %d\n", h->pcmcia->dv_xname, irq); 241 242 return (ih); 243 } 244 245 void 246 pcic_pci_chip_intr_disestablish(pch, ih) 247 pcmcia_chipset_handle_t pch; 248 void *ih; 249 { 250 struct pcic_handle *h = (struct pcic_handle *) pch; 251 252 pci_intr_disestablish(h->sc->intr_est, ih); 253 } 254