1 /* $OpenBSD: amd756.c,v 1.5 2008/06/26 05:42:11 ray Exp $ */ 2 /* $NetBSD$ */ 3 4 /*- 5 * Copyright (c) 1999 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 10 * NASA Ames Research Center. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * Copyright (c) 1999, by UCHIYAMA Yasushi 36 * All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. The name of the developer may NOT be used to endorse or promote products 44 * derived from this software without specific prior written permission. 45 * 46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 49 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 56 * SUCH DAMAGE. 57 */ 58 59 /* 60 * Support for the Advanced Micro Devices AMD756 Peripheral Bus Controller. 61 */ 62 63 #include <sys/param.h> 64 #include <sys/systm.h> 65 #include <sys/device.h> 66 #include <sys/malloc.h> 67 68 #include <machine/intr.h> 69 #include <machine/bus.h> 70 71 #include <dev/pci/pcivar.h> 72 #include <dev/pci/pcireg.h> 73 #include <dev/pci/pcidevs.h> 74 75 #include <i386/pci/pcibiosvar.h> 76 #include <i386/pci/amd756reg.h> 77 78 struct viper_handle { 79 bus_space_tag_t ph_iot; 80 bus_space_handle_t ph_regs_ioh; 81 pci_chipset_tag_t ph_pc; 82 pcitag_t ph_tag; 83 }; 84 85 int amd756_getclink(pciintr_icu_handle_t, int, int *); 86 int amd756_get_intr(pciintr_icu_handle_t, int, int *); 87 int amd756_set_intr(pciintr_icu_handle_t, int, int); 88 int amd756_get_trigger(pciintr_icu_handle_t, int, int *); 89 int amd756_set_trigger(pciintr_icu_handle_t, int, int); 90 #ifdef VIPER_DEBUG 91 static void amd756_pir_dump(struct viper_handle *); 92 #endif 93 94 const struct pciintr_icu amd756_pci_icu = { 95 amd756_getclink, 96 amd756_get_intr, 97 amd756_set_intr, 98 amd756_get_trigger, 99 amd756_set_trigger, 100 }; 101 102 103 int 104 amd756_init(pci_chipset_tag_t pc, bus_space_tag_t iot, pcitag_t tag, 105 pciintr_icu_tag_t *ptagp, pciintr_icu_handle_t *phandp) 106 { 107 struct viper_handle *ph; 108 109 ph = malloc(sizeof(*ph), M_DEVBUF, M_NOWAIT); 110 if (ph == NULL) 111 return (1); 112 113 ph->ph_iot = iot; 114 ph->ph_pc = pc; 115 ph->ph_tag = tag; 116 117 *ptagp = &amd756_pci_icu; 118 *phandp = ph; 119 120 #ifdef VIPER_DEBUG 121 amd756_pir_dump(ph); 122 #endif 123 124 return 0; 125 } 126 127 int 128 amd756_getclink(pciintr_icu_handle_t v, int link, int *clinkp) 129 { 130 if (AMD756_LEGAL_LINK(link - 1) == 0) 131 return (1); 132 133 *clinkp = link - 1; 134 return (0); 135 } 136 137 int 138 amd756_get_intr(pciintr_icu_handle_t v, int clink, int *irqp) 139 { 140 struct viper_handle *ph = v; 141 pcireg_t reg; 142 int val; 143 144 if (AMD756_LEGAL_LINK(clink) == 0) 145 return (1); 146 147 reg = AMD756_GET_PIIRQSEL(ph); 148 val = (reg >> (4*clink)) & 0x0f; 149 *irqp = (val == 0) ? 150 I386_PCI_INTERRUPT_LINE_NO_CONNECTION : val; 151 152 return (0); 153 } 154 155 int 156 amd756_set_intr(pciintr_icu_handle_t v, int clink, int irq) 157 { 158 struct viper_handle *ph = v; 159 int val; 160 pcireg_t reg; 161 162 if (AMD756_LEGAL_LINK(clink) == 0 || AMD756_LEGAL_IRQ(irq) == 0) 163 return (1); 164 165 reg = AMD756_GET_PIIRQSEL(ph); 166 amd756_get_intr(v, clink, &val); 167 reg &= ~(0x000f << (4*clink)); 168 reg |= irq << (4*clink); 169 AMD756_SET_PIIRQSEL(ph, reg); 170 171 return 0; 172 } 173 174 int 175 amd756_get_trigger(pciintr_icu_handle_t v, int irq, int *triggerp) 176 { 177 struct viper_handle *ph = v; 178 int i, pciirq; 179 pcireg_t reg; 180 181 if (AMD756_LEGAL_IRQ(irq) == 0) 182 return (1); 183 184 for (i = 0; i <= 3; i++) { 185 amd756_get_intr(v, i, &pciirq); 186 if (pciirq == irq) { 187 reg = AMD756_GET_EDGESEL(ph); 188 if (reg & (1 << i)) 189 *triggerp = IST_EDGE; 190 else 191 *triggerp = IST_LEVEL; 192 break; 193 } 194 } 195 196 return 0; 197 } 198 199 int 200 amd756_set_trigger(pciintr_icu_handle_t v, int irq, int trigger) 201 { 202 struct viper_handle *ph = v; 203 int i, pciirq; 204 pcireg_t reg; 205 206 if (AMD756_LEGAL_IRQ(irq) == 0) 207 return (1); 208 209 for (i = 0; i <= 3; i++) { 210 amd756_get_intr(v, i, &pciirq); 211 if (pciirq == irq) { 212 reg = AMD756_GET_PIIRQSEL(ph); 213 if (trigger == IST_LEVEL) 214 reg &= ~(1 << (4*i)); 215 else 216 reg |= 1 << (4*i); 217 AMD756_SET_PIIRQSEL(ph, reg); 218 break; 219 } 220 } 221 222 return (0); 223 } 224 225 #ifdef VIPER_DEBUG 226 static void 227 amd756_pir_dump(struct viper_handle *ph) 228 { 229 int a, b; 230 231 printf ("VIPER PCI INTERRUPT ROUTING REGISTERS:\n"); 232 233 a = AMD756_GET_EDGESEL(ph); 234 b = AMD756_GET_PIIRQSEL(ph); 235 236 printf ("TRIGGER: %02x, ROUTING: %04x\n", a, b); 237 } 238 #endif 239