1 /* $NetBSD: lca_pci.c,v 1.23 2021/05/07 16:58:34 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1995, 1996 Carnegie-Mellon University. 5 * All rights reserved. 6 * 7 * Author: Chris G. Demetriou 8 * 9 * Permission to use, copy, modify and distribute this software and 10 * its documentation is hereby granted, provided that both the copyright 11 * notice and this permission notice appear in all copies of the 12 * software, derivative works or modified versions, and any portions 13 * thereof, and that both notices appear in supporting documentation. 14 * 15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 18 * 19 * Carnegie Mellon requests users of this software to return to 20 * 21 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 22 * School of Computer Science 23 * Carnegie Mellon University 24 * Pittsburgh PA 15213-3890 25 * 26 * any improvements or extensions that they make and grant Carnegie the 27 * rights to redistribute these changes. 28 */ 29 30 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 31 32 __KERNEL_RCSID(0, "$NetBSD: lca_pci.c,v 1.23 2021/05/07 16:58:34 thorpej Exp $"); 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/device.h> 38 39 #include <dev/pci/pcireg.h> 40 #include <dev/pci/pcivar.h> 41 #include <alpha/pci/lcareg.h> 42 #include <alpha/pci/lcavar.h> 43 44 static void lca_attach_hook(device_t, device_t, 45 struct pcibus_attach_args *); 46 static int lca_bus_maxdevs(void *, int); 47 static pcitag_t lca_make_tag(void *, int, int, int); 48 static void lca_decompose_tag(void *, pcitag_t, int *, int *, int *); 49 static pcireg_t lca_conf_read(void *, pcitag_t, int); 50 static void lca_conf_write(void *, pcitag_t, int, pcireg_t); 51 52 void 53 lca_pci_init(pci_chipset_tag_t pc, void *v) 54 { 55 56 pc->pc_conf_v = v; 57 pc->pc_attach_hook = lca_attach_hook; 58 pc->pc_bus_maxdevs = lca_bus_maxdevs; 59 pc->pc_make_tag = lca_make_tag; 60 pc->pc_decompose_tag = lca_decompose_tag; 61 pc->pc_conf_read = lca_conf_read; 62 pc->pc_conf_write = lca_conf_write; 63 } 64 65 static void 66 lca_attach_hook(device_t parent, device_t self, struct pcibus_attach_args *pba) 67 { 68 } 69 70 static int 71 lca_bus_maxdevs(void *cpv, int busno) 72 { 73 74 if (busno == 0) 75 return 16; 76 else 77 return 32; 78 } 79 80 static pcitag_t 81 lca_make_tag(void *cpv, int b, int d, int f) 82 { 83 84 return (b << 16) | (d << 11) | (f << 8); 85 } 86 87 static void 88 lca_decompose_tag(void *cpv, pcitag_t tag, int *bp, int *dp, int *fp) 89 { 90 91 if (bp != NULL) 92 *bp = (tag >> 16) & 0xff; 93 if (dp != NULL) 94 *dp = (tag >> 11) & 0x1f; 95 if (fp != NULL) 96 *fp = (tag >> 8) & 0x7; 97 } 98 99 static pcireg_t 100 lca_conf_read(void *cpv, pcitag_t tag, int offset) 101 { 102 struct lca_config *lcp = cpv; 103 pcireg_t *datap, data; 104 int s, secondary, device, ba; 105 106 if ((unsigned int)offset >= PCI_CONF_SIZE) 107 return (pcireg_t) -1; 108 109 s = 0; /* XXX gcc -Wuninitialized */ 110 111 /* secondary if bus # != 0 */ 112 pci_decompose_tag(&lcp->lc_pc, tag, &secondary, &device, 0); 113 if (secondary) { 114 s = splhigh(); 115 alpha_mb(); 116 REGVAL(LCA_IOC_CONF) = 0x01; 117 alpha_mb(); 118 } else { 119 /* 120 * on the LCA, must frob the tag used for 121 * devices on the primary bus, in the same ways 122 * as is used by type 1 configuration cycles 123 * on PCs. 124 */ 125 tag = (1 << (device + 11)) | (tag & 0x7ff); 126 } 127 128 datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(LCA_PCI_CONF | 129 tag << 5UL | /* XXX */ 130 (offset & ~0x03) << 5 | /* XXX */ 131 0 << 5 | /* XXX */ 132 0x3 << 3); /* XXX */ 133 data = (pcireg_t)-1; 134 if (!(ba = badaddr(datap, sizeof *datap))) 135 data = *datap; 136 137 if (secondary) { 138 alpha_mb(); 139 REGVAL(LCA_IOC_CONF) = 0x00; 140 alpha_mb(); 141 splx(s); 142 } 143 144 #if 0 145 printf("lca_conf_read: tag 0x%lx, reg 0x%lx -> %x @ %p%s\n", tag, reg, 146 data, datap, ba ? " (badaddr)" : ""); 147 #endif 148 149 return data; 150 } 151 152 static void 153 lca_conf_write(void *cpv, pcitag_t tag, int offset, pcireg_t data) 154 { 155 struct lca_config *lcp = cpv; 156 pcireg_t *datap; 157 int s, secondary, device; 158 159 if ((unsigned int)offset >= PCI_CONF_SIZE) 160 return; 161 162 s = 0; /* XXX gcc -Wuninitialized */ 163 164 /* secondary if bus # != 0 */ 165 pci_decompose_tag(&lcp->lc_pc, tag, &secondary, &device, 0); 166 if (secondary) { 167 s = splhigh(); 168 alpha_mb(); 169 REGVAL(LCA_IOC_CONF) = 0x01; 170 alpha_mb(); 171 } else { 172 /* 173 * on the LCA, must frob the tag used for 174 * devices on the primary bus, in the same ways 175 * as is used by type 1 configuration cycles 176 * on PCs. 177 */ 178 tag = (1 << (device + 11)) | (tag & 0x7ff); 179 } 180 181 datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(LCA_PCI_CONF | 182 tag << 5UL | /* XXX */ 183 (offset & ~0x03) << 5 | /* XXX */ 184 0 << 5 | /* XXX */ 185 0x3 << 3); /* XXX */ 186 *datap = data; 187 188 if (secondary) { 189 alpha_mb(); 190 REGVAL(LCA_IOC_CONF) = 0x00; 191 alpha_mb(); 192 splx(s); 193 } 194 195 #if 0 196 printf("lca_conf_write: tag 0x%lx, reg 0x%lx -> 0x%x @ %p\n", tag, 197 reg, data, datap); 198 #endif 199 } 200