1 1.8 mrg /* $NetBSD: sbbrz_pci.c,v 1.8 2017/07/24 09:56:46 mrg Exp $ */ 2 1.2 matt 3 1.2 matt /* 4 1.2 matt * Copyright 2000, 2001 5 1.2 matt * Broadcom Corporation. All rights reserved. 6 1.2 matt * 7 1.2 matt * This software is furnished under license and may be used and copied only 8 1.2 matt * in accordance with the following terms and conditions. Subject to these 9 1.2 matt * conditions, you may download, copy, install, use, modify and distribute 10 1.2 matt * modified or unmodified copies of this software in source and/or binary 11 1.2 matt * form. No title or ownership is transferred hereby. 12 1.2 matt * 13 1.2 matt * 1) Any source code used, modified or distributed must reproduce and 14 1.2 matt * retain this copyright notice and list of conditions as they appear in 15 1.2 matt * the source file. 16 1.2 matt * 17 1.2 matt * 2) No right is granted to use any trade name, trademark, or logo of 18 1.2 matt * Broadcom Corporation. Neither the "Broadcom Corporation" name nor any 19 1.2 matt * trademark or logo of Broadcom Corporation may be used to endorse or 20 1.2 matt * promote products derived from this software without the prior written 21 1.2 matt * permission of Broadcom Corporation. 22 1.2 matt * 23 1.2 matt * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR IMPLIED 24 1.2 matt * WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES OF 25 1.2 matt * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR 26 1.2 matt * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM BE LIABLE 27 1.2 matt * FOR ANY DAMAGES WHATSOEVER, AND IN PARTICULAR, BROADCOM SHALL NOT BE 28 1.2 matt * LIABLE FOR DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 1.2 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 1.2 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 31 1.2 matt * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 32 1.2 matt * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 33 1.2 matt * OR OTHERWISE), EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 1.2 matt */ 35 1.2 matt 36 1.2 matt /* from: $NetBSD: apecs_pci.c,v 1.18 2000/06/29 08:58:45 mrg Exp */ 37 1.2 matt 38 1.2 matt /* 39 1.2 matt * Copyright (c) 1995, 1996 Carnegie-Mellon University. 40 1.2 matt * All rights reserved. 41 1.2 matt * 42 1.2 matt * Author: Chris G. Demetriou 43 1.2 matt * 44 1.2 matt * Permission to use, copy, modify and distribute this software and 45 1.2 matt * its documentation is hereby granted, provided that both the copyright 46 1.2 matt * notice and this permission notice appear in all copies of the 47 1.2 matt * software, derivative works or modified versions, and any portions 48 1.2 matt * thereof, and that both notices appear in supporting documentation. 49 1.2 matt * 50 1.2 matt * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 51 1.2 matt * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 52 1.2 matt * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 53 1.2 matt * 54 1.2 matt * Carnegie Mellon requests users of this software to return to 55 1.2 matt * 56 1.2 matt * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU 57 1.2 matt * School of Computer Science 58 1.2 matt * Carnegie Mellon University 59 1.2 matt * Pittsburgh PA 15213-3890 60 1.2 matt * 61 1.2 matt * any improvements or extensions that they make and grant Carnegie the 62 1.2 matt * rights to redistribute these changes. 63 1.2 matt */ 64 1.2 matt 65 1.2 matt #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 66 1.2 matt 67 1.8 mrg __KERNEL_RCSID(0, "$NetBSD: sbbrz_pci.c,v 1.8 2017/07/24 09:56:46 mrg Exp $"); 68 1.2 matt 69 1.2 matt #include <sys/param.h> 70 1.2 matt #include <sys/systm.h> 71 1.2 matt #include <sys/kernel.h> 72 1.2 matt #include <sys/device.h> 73 1.2 matt #include <uvm/uvm_extern.h> 74 1.2 matt 75 1.2 matt #include <dev/pci/pcireg.h> 76 1.2 matt #include <dev/pci/pcivar.h> 77 1.2 matt 78 1.4 matt #include <mips/locore.h> 79 1.2 matt #include <mips/sibyte/include/sb1250_regs.h> 80 1.2 matt #include <mips/sibyte/include/sb1250_scd.h> 81 1.2 matt #include <mips/sibyte/include/sb1250_int.h> 82 1.2 matt #include <mips/sibyte/pci/sbbrzvar.h> 83 1.2 matt 84 1.8 mrg #include <evbmips/sbmips/systemsw.h> 85 1.8 mrg 86 1.2 matt void sbbrz_pci_attach_hook(device_t, device_t, 87 1.2 matt struct pcibus_attach_args *); 88 1.2 matt static int sbbrz_pci_bus_maxdevs(void *, int); 89 1.2 matt static pcitag_t sbbrz_pci_make_tag(void *, int, int, int); 90 1.2 matt static void sbbrz_pci_decompose_tag(void *, pcitag_t, int *, int *, int *); 91 1.2 matt 92 1.2 matt static pcireg_t sbbrz_pci_conf_read(void *, pcitag_t, int); 93 1.2 matt static void sbbrz_pci_conf_write(void *, pcitag_t, int, pcireg_t); 94 1.2 matt #ifdef PCI_NETBSD_CONFIGURE 95 1.2 matt static void sbbrz_pci_conf_interrupt(void *, int, int, int, int, int *); 96 1.2 matt #endif 97 1.2 matt 98 1.3 dyoung static int sbbrz_pci_intr_map(const struct pci_attach_args *, 99 1.2 matt pci_intr_handle_t *); 100 1.2 matt static const char * 101 1.5 christos sbbrz_pci_intr_string(void *, pci_intr_handle_t, char *, size_t); 102 1.2 matt static const struct evcnt * 103 1.2 matt sbbrz_pci_intr_evcnt(void *, pci_intr_handle_t); 104 1.2 matt static void * sbbrz_pci_intr_establish(void *, pci_intr_handle_t, 105 1.2 matt int, int (*)(void *), void *); 106 1.2 matt static void sbbrz_pci_intr_disestablish(void *, void *); 107 1.2 matt 108 1.2 matt 109 1.2 matt void 110 1.2 matt sbbrz_pci_init(pci_chipset_tag_t pc, void *v) 111 1.2 matt { 112 1.2 matt 113 1.2 matt pc->pc_conf_v = v; 114 1.2 matt pc->pc_attach_hook = sbbrz_pci_attach_hook; 115 1.2 matt pc->pc_bus_maxdevs = sbbrz_pci_bus_maxdevs; 116 1.2 matt pc->pc_make_tag = sbbrz_pci_make_tag; 117 1.2 matt pc->pc_decompose_tag = sbbrz_pci_decompose_tag; 118 1.2 matt pc->pc_conf_read = sbbrz_pci_conf_read; 119 1.2 matt pc->pc_conf_write = sbbrz_pci_conf_write; 120 1.2 matt pc->pc_intr_map = sbbrz_pci_intr_map; 121 1.2 matt pc->pc_intr_string = sbbrz_pci_intr_string; 122 1.2 matt pc->pc_intr_evcnt = sbbrz_pci_intr_evcnt; 123 1.2 matt pc->pc_intr_establish = sbbrz_pci_intr_establish; 124 1.2 matt pc->pc_intr_disestablish = sbbrz_pci_intr_disestablish; 125 1.2 matt #ifdef PCI_NETBSD_CONFIGURE 126 1.2 matt pc->pc_conf_interrupt = sbbrz_pci_conf_interrupt; 127 1.2 matt #endif 128 1.2 matt #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH 129 1.2 matt pc->pc_pciide_compat_intr_establish = sbbrz_pciide_compat_intr_establish; 130 1.2 matt #endif 131 1.2 matt } 132 1.2 matt 133 1.2 matt void 134 1.2 matt sbbrz_pci_attach_hook(device_t parent, device_t self, 135 1.2 matt struct pcibus_attach_args *pba) 136 1.2 matt { 137 1.2 matt } 138 1.2 matt 139 1.2 matt int 140 1.2 matt sbbrz_pci_bus_maxdevs(void *cpv, int busno) 141 1.2 matt { 142 1.2 matt uint64_t regval; 143 1.2 matt int host; 144 1.2 matt 145 1.2 matt /* If not the PCI bus directly off the 1250, always up to 32 devs. */ 146 1.2 matt if (busno != 0) 147 1.2 matt return 32; 148 1.2 matt 149 1.2 matt /* If the PCI on the 1250, 32 devices if host mode, otherwise only 2. */ 150 1.7 christos regval = mips3_ld((register_t)MIPS_PHYS_TO_KSEG1(A_SCD_SYSTEM_CFG)); 151 1.2 matt host = (regval & M_SYS_PCI_HOST) != 0; 152 1.2 matt 153 1.2 matt return (host ? 32 : 2); 154 1.2 matt } 155 1.2 matt 156 1.2 matt pcitag_t 157 1.2 matt sbbrz_pci_make_tag(void *cpv, int b, int d, int f) 158 1.2 matt { 159 1.2 matt 160 1.2 matt return (b << 16) | (d << 11) | (f << 8); 161 1.2 matt } 162 1.2 matt 163 1.2 matt void 164 1.2 matt sbbrz_pci_decompose_tag(void *cpv, pcitag_t tag, 165 1.2 matt int *bp, int *dp, int *fp) 166 1.2 matt { 167 1.2 matt 168 1.2 matt if (bp != NULL) 169 1.2 matt *bp = (tag >> 16) & 0xff; 170 1.2 matt if (dp != NULL) 171 1.2 matt *dp = (tag >> 11) & 0x1f; 172 1.2 matt if (fp != NULL) 173 1.2 matt *fp = (tag >> 8) & 0x7; 174 1.2 matt } 175 1.2 matt 176 1.2 matt pcireg_t 177 1.2 matt sbbrz_pci_conf_read(void *cpv, pcitag_t tag, int offset) 178 1.2 matt { 179 1.2 matt uint64_t addr; 180 1.2 matt 181 1.2 matt #ifdef DIAGNOSTIC 182 1.2 matt if ((offset & 0x3) != 0) 183 1.2 matt panic ("pci_conf_read: misaligned"); 184 1.2 matt #endif 185 1.2 matt 186 1.6 msaitoh if ((unsigned int)offset >= PCI_CONF_SIZE) 187 1.6 msaitoh return 0xffffffff; 188 1.6 msaitoh 189 1.2 matt addr = A_PHYS_LDTPCI_CFG_MATCH_BITS + tag + offset; 190 1.2 matt addr = MIPS_PHYS_TO_XKPHYS(MIPS3_TLB_ATTR_UNCACHED, addr); 191 1.2 matt 192 1.2 matt __asm volatile("sync"); 193 1.2 matt 194 1.2 matt if (badaddr64(addr, 4) != 0) 195 1.2 matt return 0xffffffff; 196 1.2 matt 197 1.7 christos return mips3_ld(addr); 198 1.2 matt } 199 1.2 matt 200 1.2 matt void 201 1.2 matt sbbrz_pci_conf_write(void *cpv, pcitag_t tag, int offset, pcireg_t data) 202 1.2 matt { 203 1.2 matt uint64_t addr; 204 1.2 matt 205 1.2 matt #ifdef DIAGNOSTIC 206 1.2 matt if ((offset & 0x3) != 0) 207 1.2 matt panic ("pci_conf_write: misaligned"); 208 1.2 matt #endif 209 1.2 matt 210 1.6 msaitoh if ((unsigned int)offset >= PCI_CONF_SIZE) 211 1.6 msaitoh return; 212 1.6 msaitoh 213 1.2 matt addr = A_PHYS_LDTPCI_CFG_MATCH_BITS + tag + offset; 214 1.2 matt addr = MIPS_PHYS_TO_XKPHYS(MIPS3_TLB_ATTR_UNCACHED, addr); 215 1.2 matt 216 1.7 christos return mips3_sd(addr, data); 217 1.2 matt } 218 1.2 matt 219 1.2 matt int 220 1.3 dyoung sbbrz_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp) 221 1.2 matt { 222 1.2 matt int bus, device, func; 223 1.2 matt sbbrz_pci_decompose_tag(NULL, pa->pa_intrtag, &bus, &device, &func); 224 1.2 matt *ihp = 0; 225 1.2 matt if (pa->pa_intrpin == PCI_INTERRUPT_PIN_NONE) 226 1.2 matt return EINVAL; 227 1.2 matt if (bus == 0) { 228 1.2 matt *ihp = K_INT_PCI_INTA 229 1.2 matt + (((device-5) + pa->pa_intrswiz + pa->pa_intrpin - PCI_INTERRUPT_PIN_A) % 4); 230 1.2 matt return 0; 231 1.2 matt } 232 1.2 matt return EOPNOTSUPP; 233 1.2 matt } 234 1.2 matt 235 1.2 matt const char * 236 1.5 christos sbbrz_pci_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len) 237 1.2 matt { 238 1.5 christos char c; 239 1.5 christos 240 1.2 matt switch (ih) { 241 1.5 christos default: c = '?'; break; 242 1.5 christos case K_INT_PCI_INTA: c = 'a'; break; 243 1.5 christos case K_INT_PCI_INTB: c = 'b'; break; 244 1.5 christos case K_INT_PCI_INTC: c = 'c'; break; 245 1.5 christos case K_INT_PCI_INTD: c = 'd'; break; 246 1.2 matt } 247 1.5 christos snprintf(buf, len, "pci int%c", c); 248 1.5 christos return buf; 249 1.2 matt } 250 1.2 matt 251 1.2 matt const struct evcnt * 252 1.2 matt sbbrz_pci_intr_evcnt(void *v, pci_intr_handle_t ih) 253 1.2 matt { 254 1.2 matt return NULL; 255 1.2 matt } 256 1.2 matt 257 1.2 matt void * 258 1.2 matt sbbrz_pci_intr_establish(void *v, pci_intr_handle_t ih, int level, 259 1.2 matt int (*handler)(void *), void *arg) 260 1.2 matt { 261 1.2 matt return cpu_intr_establish(ih, level, 262 1.2 matt (void (*)(void *, uint32_t, vaddr_t))handler, arg); 263 1.2 matt } 264 1.2 matt 265 1.2 matt void 266 1.2 matt sbbrz_pci_intr_disestablish(void *v, void *ih) 267 1.2 matt { 268 1.2 matt } 269