1 /* $NetBSD: dio.c,v 1.7 1997/05/05 21:00:32 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Autoconfiguration and mapping support for the DIO bus. 41 */ 42 43 #define _HP300_INTR_H_PRIVATE 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/device.h> 48 #include <sys/kernel.h> 49 #include <sys/device.h> 50 51 #include <machine/autoconf.h> 52 #include <machine/cpu.h> 53 #include <machine/hp300spu.h> 54 55 #include <hp300/dev/dmavar.h> 56 57 #include <hp300/dev/dioreg.h> 58 #include <hp300/dev/diovar.h> 59 60 #include <hp300/dev/diodevs.h> 61 #include <hp300/dev/diodevs_data.h> 62 63 extern caddr_t internalhpib; 64 65 int dio_scodesize __P((struct dio_attach_args *)); 66 char *dio_devinfo __P((struct dio_attach_args *, char *, size_t)); 67 68 int diomatch __P((struct device *, struct cfdata *, void *)); 69 void dioattach __P((struct device *, struct device *, void *)); 70 int dioprint __P((void *, const char *)); 71 int diosubmatch __P((struct device *, struct cfdata *, void *)); 72 73 struct cfattach dio_ca = { 74 sizeof(struct device), diomatch, dioattach 75 }; 76 77 struct cfdriver dio_cd = { 78 NULL, "dio", DV_DULL 79 }; 80 81 int 82 diomatch(parent, match, aux) 83 struct device *parent; 84 struct cfdata *match; 85 void *aux; 86 { 87 static int dio_matched = 0; 88 89 /* Allow only one instance. */ 90 if (dio_matched) 91 return (0); 92 93 dio_matched = 1; 94 return (1); 95 } 96 97 void 98 dioattach(parent, self, aux) 99 struct device *parent, *self; 100 void *aux; 101 { 102 struct dio_attach_args da; 103 caddr_t pa, va; 104 int scode, scmax, didmap, scodesize; 105 106 scmax = DIO_SCMAX(machineid); 107 printf(": "); 108 dmainit(); 109 110 for (scode = 0; scode < scmax; ) { 111 if (DIO_INHOLE(scode)) { 112 scode++; 113 continue; 114 } 115 116 didmap = 0; 117 118 /* 119 * Temporarily map the space corresponding to 120 * the current select code unless: 121 * - this is the internal hpib select code, 122 * - this is the console select code. 123 */ 124 pa = dio_scodetopa(scode); 125 if (scode == conscode) 126 va = conaddr; 127 else if ((scode == 7) && internalhpib) 128 va = internalhpib = (caddr_t)IIOV(pa); 129 else { 130 va = iomap(pa, NBPG); 131 if (va == NULL) { 132 printf("%s: can't map scode %d\n", 133 self->dv_xname, scode); 134 scode++; 135 continue; 136 } 137 didmap = 1; 138 } 139 140 /* Check for hardware. */ 141 if (badaddr(va)) { 142 if (didmap) 143 iounmap(va, NBPG); 144 scode++; 145 continue; 146 } 147 148 /* Fill out attach args. */ 149 bzero(&da, sizeof(da)); 150 da.da_scode = scode; 151 da.da_id = DIO_ID(va); 152 153 if (DIO_ISFRAMEBUFFER(da.da_id)) 154 da.da_secid = DIO_SECID(va); 155 156 da.da_size = DIO_SIZE(scode, va); 157 scodesize = dio_scodesize(&da); 158 if (DIO_ISDIO(scode)) 159 da.da_size *= scodesize; 160 161 /* No longer need the device to be mapped. */ 162 if (didmap) 163 iounmap(va, NBPG); 164 165 /* Attach matching device. */ 166 config_found_sm(self, &da, dioprint, diosubmatch); 167 scode += scodesize; 168 } 169 } 170 171 int 172 diosubmatch(parent, cf, aux) 173 struct device *parent; 174 struct cfdata *cf; 175 void *aux; 176 { 177 struct dio_attach_args *da = aux; 178 179 if (cf->diocf_scode != DIO_UNKNOWN_SCODE && 180 cf->diocf_scode != da->da_scode) 181 return (0); 182 183 return ((*cf->cf_attach->ca_match)(parent, cf, aux)); 184 } 185 186 int 187 dioprint(aux, pnp) 188 void *aux; 189 const char *pnp; 190 { 191 struct dio_attach_args *da = aux; 192 char buf[128]; 193 194 if (pnp) 195 printf("%s at %s", dio_devinfo(da, buf, sizeof(buf)), pnp); 196 printf(" scode %d", da->da_scode); 197 return (UNCONF); 198 } 199 200 /* 201 * Convert a select code to a system physical address. 202 */ 203 void * 204 dio_scodetopa(scode) 205 int scode; 206 { 207 u_long rval; 208 209 if (scode == 7 && internalhpib) 210 rval = DIO_IHPIBADDR; 211 else if (DIO_ISDIO(scode)) 212 rval = DIO_BASE + (scode * DIO_DEVSIZE); 213 else if (DIO_ISDIOII(scode)) 214 rval = DIOII_BASE + ((scode - DIOII_SCBASE) * DIOII_DEVSIZE); 215 else 216 rval = 0; 217 218 return ((void *)rval); 219 } 220 221 /* 222 * Return the select code size for this device, defaulting to 1 223 * if we don't know what kind of device we have. 224 */ 225 int 226 dio_scodesize(da) 227 struct dio_attach_args *da; 228 { 229 int i; 230 231 /* 232 * Deal with lame internal HP-IB controllers which don't have 233 * consistent/reliable device ids. 234 */ 235 if (da->da_scode == 7 && internalhpib) 236 return (1); 237 238 /* 239 * Find the dio_devdata matchind the primary id. 240 * If we're a framebuffer, we also check the secondary id. 241 */ 242 for (i = 0; i < DIO_NDEVICES; i++) { 243 if (da->da_id == dio_devdatas[i].dd_id) { 244 if (DIO_ISFRAMEBUFFER(da->da_id)) { 245 if (da->da_secid == dio_devdatas[i].dd_secid) { 246 goto foundit; 247 } 248 } else { 249 foundit: 250 return (dio_devdatas[i].dd_nscode); 251 } 252 } 253 } 254 255 /* 256 * Device is unknown. Print a warning and assume a default. 257 */ 258 printf("WARNING: select code size unknown for id = 0x%x secid = 0x%x\n", 259 da->da_id, da->da_secid); 260 return (1); 261 } 262 263 /* 264 * Return a reasonable description of a DIO device. 265 */ 266 char * 267 dio_devinfo(da, buf, buflen) 268 struct dio_attach_args *da; 269 char *buf; 270 size_t buflen; 271 { 272 #ifdef DIOVERBOSE 273 int i; 274 #endif 275 276 bzero(buf, buflen); 277 278 /* 279 * Deal with lame internal HP-IB controllers which don't have 280 * consistent/reliable device ids. 281 */ 282 if (da->da_scode == 7 && internalhpib) { 283 sprintf(buf, DIO_DEVICE_DESC_IHPIB); 284 return (buf); 285 } 286 287 #ifdef DIOVERBOSE 288 /* 289 * Find the description matching our primary id. 290 * If we're a framebuffer, we also check the secondary id. 291 */ 292 for (i = 0; i < DIO_NDEVICES; i++) { 293 if (da->da_id == dio_devdescs[i].dd_id) { 294 if (DIO_ISFRAMEBUFFER(da->da_id)) { 295 if (da->da_secid == dio_devdescs[i].dd_secid) { 296 goto foundit; 297 } 298 } else { 299 foundit: 300 sprintf(buf, "%s", dio_devdescs[i].dd_desc); 301 return (buf); 302 } 303 } 304 } 305 #endif /* DIOVERBOSE */ 306 307 /* 308 * Device is unknown. Construct something reasonable. 309 */ 310 sprintf(buf, "device id = 0x%x secid = 0x%x", 311 da->da_id, da->da_secid); 312 return (buf); 313 } 314 315 /* 316 * Establish an interrupt handler for a DIO device. 317 */ 318 void * 319 dio_intr_establish(func, arg, ipl, priority) 320 int (*func) __P((void *)); 321 void *arg; 322 int ipl; 323 int priority; 324 { 325 void *ih; 326 327 ih = intr_establish(func, arg, ipl, priority); 328 329 if (priority == IPL_BIO) 330 dmacomputeipl(); 331 332 return (ih); 333 } 334 335 /* 336 * Remove an interrupt handler for a DIO device. 337 */ 338 void 339 dio_intr_disestablish(arg) 340 void *arg; 341 { 342 struct isr *isr = arg; 343 int priority = isr->isr_priority; 344 345 intr_disestablish(arg); 346 347 if (priority == IPL_BIO) 348 dmacomputeipl(); 349 } 350