1 1.21 thorpej /* $NetBSD: dwlpx_pci.c,v 1.21 2021/06/25 03:46:30 thorpej Exp $ */ 2 1.1 cgd 3 1.1 cgd /* 4 1.2 cgd * Copyright (c) 1997 by Matthew Jacob 5 1.1 cgd * NASA AMES Research Center. 6 1.1 cgd * All rights reserved. 7 1.1 cgd * 8 1.1 cgd * Redistribution and use in source and binary forms, with or without 9 1.1 cgd * modification, are permitted provided that the following conditions 10 1.1 cgd * are met: 11 1.1 cgd * 1. Redistributions of source code must retain the above copyright 12 1.1 cgd * notice immediately at the beginning of the file, without modification, 13 1.1 cgd * this list of conditions, and the following disclaimer. 14 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright 15 1.1 cgd * notice, this list of conditions and the following disclaimer in the 16 1.1 cgd * documentation and/or other materials provided with the distribution. 17 1.1 cgd * 3. The name of the author may not be used to endorse or promote products 18 1.1 cgd * derived from this software without specific prior written permission. 19 1.1 cgd * 20 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 24 1.1 cgd * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 1.1 cgd * SUCH DAMAGE. 31 1.1 cgd */ 32 1.5 cgd 33 1.6 cgd #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 34 1.6 cgd 35 1.21 thorpej __KERNEL_RCSID(0, "$NetBSD: dwlpx_pci.c,v 1.21 2021/06/25 03:46:30 thorpej Exp $"); 36 1.1 cgd 37 1.1 cgd #include <sys/param.h> 38 1.1 cgd #include <sys/systm.h> 39 1.1 cgd #include <sys/kernel.h> 40 1.1 cgd #include <sys/device.h> 41 1.10 mrg 42 1.1 cgd #include <dev/pci/pcireg.h> 43 1.1 cgd #include <dev/pci/pcivar.h> 44 1.1 cgd #include <alpha/tlsb/tlsbreg.h> 45 1.1 cgd #include <alpha/pci/dwlpxreg.h> 46 1.1 cgd #include <alpha/pci/dwlpxvar.h> 47 1.1 cgd 48 1.12 christos #define KV(_addr) ((void *)ALPHA_PHYS_TO_K0SEG((_addr))) 49 1.1 cgd 50 1.20 thorpej static int dwlpx_bus_maxdevs(void *, int); 51 1.20 thorpej static pcitag_t dwlpx_make_tag(void *, int, int, int); 52 1.20 thorpej static void dwlpx_decompose_tag(void *, pcitag_t, int *, int *, 53 1.13 dsl int *); 54 1.20 thorpej static pcireg_t dwlpx_conf_read(void *, pcitag_t, int); 55 1.20 thorpej static void dwlpx_conf_write(void *, pcitag_t, int, pcireg_t); 56 1.1 cgd 57 1.1 cgd void 58 1.14 dsl dwlpx_pci_init(pci_chipset_tag_t pc, void *v) 59 1.1 cgd { 60 1.1 cgd pc->pc_conf_v = v; 61 1.1 cgd pc->pc_bus_maxdevs = dwlpx_bus_maxdevs; 62 1.1 cgd pc->pc_make_tag = dwlpx_make_tag; 63 1.1 cgd pc->pc_decompose_tag = dwlpx_decompose_tag; 64 1.1 cgd pc->pc_conf_read = dwlpx_conf_read; 65 1.1 cgd pc->pc_conf_write = dwlpx_conf_write; 66 1.1 cgd } 67 1.1 cgd 68 1.20 thorpej static int 69 1.14 dsl dwlpx_bus_maxdevs(void *cpv, int busno) 70 1.1 cgd { 71 1.1 cgd return DWLPX_MAXDEV; 72 1.1 cgd } 73 1.1 cgd 74 1.20 thorpej static pcitag_t 75 1.15 dsl dwlpx_make_tag(void *cpv, int b, int d, int f) 76 1.1 cgd { 77 1.1 cgd pcitag_t tag; 78 1.1 cgd int hpcdev, pci_idsel; 79 1.1 cgd 80 1.1 cgd pci_idsel = (1 << ((d & 0x3) + 2)); 81 1.1 cgd hpcdev = d >> 2; 82 1.1 cgd tag = (b << 24) | (hpcdev << 22) | (pci_idsel << 16) | (f << 13); 83 1.1 cgd return (tag); 84 1.1 cgd } 85 1.1 cgd 86 1.20 thorpej static void 87 1.15 dsl dwlpx_decompose_tag(void *cpv, pcitag_t tag, int *bp, int *dp, int *fp) 88 1.1 cgd { 89 1.1 cgd 90 1.1 cgd if (bp != NULL) 91 1.1 cgd *bp = (tag >> 24) & 0xff; 92 1.1 cgd if (dp != NULL) { 93 1.1 cgd int j, i = (tag >> 18) & 0xf; 94 1.1 cgd j = -1; 95 1.1 cgd while (i != 0) { 96 1.1 cgd j++; 97 1.1 cgd i >>= 1; 98 1.1 cgd } 99 1.1 cgd j += (((tag >> 22) & 3) << 2); 100 1.1 cgd *dp = j; 101 1.1 cgd } 102 1.1 cgd if (fp != NULL) 103 1.1 cgd *fp = (tag >> 13) & 0x7; 104 1.1 cgd } 105 1.1 cgd 106 1.20 thorpej static pcireg_t 107 1.14 dsl dwlpx_conf_read(void *cpv, pcitag_t tag, int offset) 108 1.1 cgd { 109 1.1 cgd struct dwlpx_config *ccp = cpv; 110 1.1 cgd struct dwlpx_softc *sc; 111 1.1 cgd pcireg_t *dp, data = (pcireg_t) -1; 112 1.1 cgd unsigned long paddr; 113 1.1 cgd int secondary, i, s = 0; 114 1.18 matt uint32_t rvp; 115 1.1 cgd 116 1.19 msaitoh if ((unsigned int)offset >= PCI_CONF_SIZE) 117 1.19 msaitoh return (data); 118 1.19 msaitoh 119 1.1 cgd if (ccp == NULL) { 120 1.11 provos panic("NULL ccp in dwlpx_conf_read"); 121 1.1 cgd } 122 1.1 cgd sc = ccp->cc_sc; 123 1.1 cgd secondary = tag >> 24; 124 1.1 cgd if (secondary) { 125 1.1 cgd tag &= 0x1fffff; 126 1.1 cgd tag |= (secondary << 21); 127 1.1 cgd 128 1.4 cgd #if 0 129 1.1 cgd printf("read secondary %d reg %x (tag %x)", 130 1.1 cgd secondary, offset, tag); 131 1.1 cgd #endif 132 1.1 cgd 133 1.1 cgd alpha_pal_draina(); 134 1.1 cgd s = splhigh(); 135 1.1 cgd /* 136 1.1 cgd * Set up HPCs for type 1 cycles. 137 1.1 cgd */ 138 1.1 cgd for (i = 0; i < sc->dwlpx_nhpc; i++) { 139 1.7 mjacob rvp = REGVAL(PCIA_CTL(i) + ccp->cc_sysbase) | 140 1.7 mjacob PCIA_CTL_T1CYC; 141 1.1 cgd alpha_mb(); 142 1.1 cgd REGVAL(PCIA_CTL(i) + ccp->cc_sysbase) = rvp; 143 1.1 cgd alpha_mb(); 144 1.1 cgd } 145 1.1 cgd } 146 1.1 cgd paddr = (unsigned long) tag; 147 1.1 cgd paddr |= DWLPX_PCI_CONF; 148 1.1 cgd paddr |= ((unsigned long) ((offset >> 2) << 7)); 149 1.1 cgd paddr |= (((unsigned long) sc->dwlpx_hosenum) << 34); 150 1.1 cgd paddr |= (((u_long) sc->dwlpx_node - 4) << 36); 151 1.1 cgd paddr |= (1LL << 39); 152 1.9 mjacob paddr |= (3LL << 3); /* 32 Bit PCI byte enables */ 153 1.1 cgd 154 1.1 cgd dp = (pcireg_t *)KV(paddr); 155 1.1 cgd if (badaddr(dp, sizeof (*dp)) == 0) { 156 1.1 cgd data = *dp; 157 1.1 cgd } 158 1.1 cgd if (secondary) { 159 1.1 cgd alpha_pal_draina(); 160 1.1 cgd for (i = 0; i < sc->dwlpx_nhpc; i++) { 161 1.7 mjacob rvp = REGVAL(PCIA_CTL(i) + ccp->cc_sysbase) & 162 1.7 mjacob ~PCIA_CTL_T1CYC; 163 1.1 cgd alpha_mb(); 164 1.1 cgd REGVAL(PCIA_CTL(i) + ccp->cc_sysbase) = rvp; 165 1.1 cgd alpha_mb(); 166 1.1 cgd } 167 1.1 cgd (void) splx(s); 168 1.4 cgd #if 0 169 1.1 cgd printf("=%x\n", data); 170 1.1 cgd #endif 171 1.1 cgd } 172 1.1 cgd return (data); 173 1.1 cgd } 174 1.1 cgd 175 1.20 thorpej static void 176 1.14 dsl dwlpx_conf_write(void *cpv, pcitag_t tag, int offset, pcireg_t data) 177 1.1 cgd { 178 1.1 cgd struct dwlpx_config *ccp = cpv; 179 1.1 cgd struct dwlpx_softc *sc; 180 1.1 cgd pcireg_t *dp; 181 1.1 cgd unsigned long paddr; 182 1.1 cgd int secondary, i, s = 0; 183 1.18 matt uint32_t rvp; 184 1.1 cgd 185 1.19 msaitoh if ((unsigned int)offset >= PCI_CONF_SIZE) 186 1.19 msaitoh return; 187 1.19 msaitoh 188 1.1 cgd if (ccp == NULL) { 189 1.11 provos panic("NULL ccp in dwlpx_conf_write"); 190 1.1 cgd } 191 1.1 cgd sc = ccp->cc_sc; 192 1.1 cgd secondary = tag >> 24; 193 1.1 cgd if (secondary) { 194 1.1 cgd tag &= 0x1fffff; 195 1.1 cgd tag |= (secondary << 21); 196 1.4 cgd #if 0 197 1.1 cgd printf("write secondary %d reg %x (tag %x) with %x\n", 198 1.1 cgd secondary, offset, tag, data); 199 1.1 cgd #endif 200 1.1 cgd 201 1.1 cgd alpha_pal_draina(); 202 1.1 cgd s = splhigh(); 203 1.1 cgd /* 204 1.1 cgd * Set up HPCs for type 1 cycles. 205 1.1 cgd */ 206 1.1 cgd for (i = 0; i < sc->dwlpx_nhpc; i++) { 207 1.7 mjacob rvp = REGVAL(PCIA_CTL(i) + ccp->cc_sysbase) | 208 1.7 mjacob PCIA_CTL_T1CYC; 209 1.1 cgd alpha_mb(); 210 1.1 cgd REGVAL(PCIA_CTL(i) + ccp->cc_sysbase) = rvp; 211 1.1 cgd alpha_mb(); 212 1.1 cgd } 213 1.1 cgd } 214 1.1 cgd paddr = (unsigned long) tag; 215 1.1 cgd paddr |= DWLPX_PCI_CONF; 216 1.1 cgd paddr |= ((unsigned long) ((offset >> 2) << 7)); 217 1.1 cgd paddr |= (((unsigned long) sc->dwlpx_hosenum) << 34); 218 1.1 cgd paddr |= (((u_long) sc->dwlpx_node - 4) << 36); 219 1.1 cgd paddr |= (1LL << 39); 220 1.9 mjacob paddr |= (3LL << 3); /* 32 bit PCI byte enables */ 221 1.1 cgd 222 1.1 cgd dp = (pcireg_t *)KV(paddr); 223 1.1 cgd *dp = data; 224 1.1 cgd alpha_mb(); 225 1.1 cgd if (secondary) { 226 1.1 cgd alpha_pal_draina(); 227 1.1 cgd for (i = 0; i < sc->dwlpx_nhpc; i++) { 228 1.7 mjacob rvp = REGVAL(PCIA_CTL(i) + ccp->cc_sysbase) & 229 1.7 mjacob ~PCIA_CTL_T1CYC; 230 1.1 cgd alpha_mb(); 231 1.1 cgd REGVAL(PCIA_CTL(i) + ccp->cc_sysbase) = rvp; 232 1.1 cgd alpha_mb(); 233 1.1 cgd } 234 1.1 cgd (void) splx(s); 235 1.1 cgd } 236 1.1 cgd } 237