1 /* $NetBSD: lca_pci.c,v 1.24 2021/06/25 03:52:41 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.24 2021/06/25 03:52:41 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 int lca_bus_maxdevs(void *, int); 45 static pcireg_t lca_conf_read(void *, pcitag_t, int); 46 static void lca_conf_write(void *, pcitag_t, int, pcireg_t); 47 48 void 49 lca_pci_init(pci_chipset_tag_t pc, void *v) 50 { 51 52 pc->pc_conf_v = v; 53 pc->pc_bus_maxdevs = lca_bus_maxdevs; 54 pc->pc_conf_read = lca_conf_read; 55 pc->pc_conf_write = lca_conf_write; 56 } 57 58 static int 59 lca_bus_maxdevs(void *cpv, int busno) 60 { 61 /* 62 * We have to drive the IDSEL directly on bus 0, so we are 63 * limited to 16 devices there. 64 */ 65 return busno == 0 ? 16 : 32; 66 } 67 68 static paddr_t 69 lca_make_type0addr(int d, int f) 70 { 71 KASSERT(d < 16); 72 return PCI_CONF_TYPE0_IDSEL(d) | __SHIFTIN(f, PCI_CONF_TYPE0_FUNCTION); 73 } 74 75 static pcireg_t 76 lca_conf_read(void *cpv, pcitag_t tag, int offset) 77 { 78 struct lca_config *lcp = cpv; 79 pcireg_t *datap, data; 80 paddr_t confaddr; 81 int s, secondary, d, f, ba; 82 83 if ((unsigned int)offset >= PCI_CONF_SIZE) 84 return (pcireg_t) -1; 85 86 s = 0; /* XXX gcc -Wuninitialized */ 87 88 /* secondary if bus # != 0 */ 89 pci_decompose_tag(&lcp->lc_pc, tag, &secondary, &d, &f); 90 if (secondary) { 91 s = splhigh(); 92 alpha_mb(); 93 REGVAL(LCA_IOC_CONF) = 0x01; 94 alpha_mb(); 95 confaddr = tag; 96 } else { 97 confaddr = lca_make_type0addr(d, f); 98 } 99 100 datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(LCA_PCI_CONF | 101 confaddr << 5UL | /* XXX */ 102 (offset & ~0x03) << 5 | /* XXX */ 103 0 << 5 | /* XXX */ 104 0x3 << 3); /* XXX */ 105 data = (pcireg_t)-1; 106 if (!(ba = badaddr(datap, sizeof *datap))) 107 data = *datap; 108 109 if (secondary) { 110 alpha_mb(); 111 REGVAL(LCA_IOC_CONF) = 0x00; 112 alpha_mb(); 113 splx(s); 114 } 115 116 #if 0 117 printf("lca_conf_read: tag 0x%lx, reg 0x%lx -> %x @ %p%s\n", tag, reg, 118 data, datap, ba ? " (badaddr)" : ""); 119 #endif 120 121 return data; 122 } 123 124 static void 125 lca_conf_write(void *cpv, pcitag_t tag, int offset, pcireg_t data) 126 { 127 struct lca_config *lcp = cpv; 128 pcireg_t *datap; 129 paddr_t confaddr; 130 int s, secondary, d, f; 131 132 if ((unsigned int)offset >= PCI_CONF_SIZE) 133 return; 134 135 s = 0; /* XXX gcc -Wuninitialized */ 136 137 /* secondary if bus # != 0 */ 138 pci_decompose_tag(&lcp->lc_pc, tag, &secondary, &d, &f); 139 if (secondary) { 140 s = splhigh(); 141 alpha_mb(); 142 REGVAL(LCA_IOC_CONF) = 0x01; 143 alpha_mb(); 144 confaddr = tag; 145 } else { 146 confaddr = lca_make_type0addr(d, f); 147 } 148 149 datap = (pcireg_t *)ALPHA_PHYS_TO_K0SEG(LCA_PCI_CONF | 150 confaddr << 5UL | /* XXX */ 151 (offset & ~0x03) << 5 | /* XXX */ 152 0 << 5 | /* XXX */ 153 0x3 << 3); /* XXX */ 154 *datap = data; 155 156 if (secondary) { 157 alpha_mb(); 158 REGVAL(LCA_IOC_CONF) = 0x00; 159 alpha_mb(); 160 splx(s); 161 } 162 163 #if 0 164 printf("lca_conf_write: tag 0x%lx, reg 0x%lx -> 0x%x @ %p\n", tag, 165 reg, data, datap); 166 #endif 167 } 168