1 /* $NetBSD: sii_ds.c,v 1.9 2011/07/09 17:32:30 matt Exp $ */ 2 3 /* 4 * Copyright 1996 The Board of Trustees of The Leland Stanford 5 * Junior University. All Rights Reserved. 6 * 7 * Permission to use, copy, modify, and distribute this 8 * software and its documentation for any purpose and without 9 * fee is hereby granted, provided that the above copyright 10 * notice appear in all copies. Stanford University 11 * makes no representations about the suitability of this 12 * software for any purpose. It is provided "as is" without 13 * express or implied warranty. 14 * 15 * this driver contributed by Jonathan Stone 16 */ 17 18 #include <sys/cdefs.h> 19 __KERNEL_RCSID(0, "$NetBSD: sii_ds.c,v 1.9 2011/07/09 17:32:30 matt Exp $"); 20 21 #include "sii.h" 22 23 #include <sys/param.h> 24 #include <sys/buf.h> 25 #include <sys/bus.h> 26 #include <sys/device.h> 27 #include <sys/systm.h> 28 29 #include <mips/locore.h> 30 #include <pmax/locore.h> 31 32 #include <dev/scsipi/scsi_all.h> 33 #include <dev/scsipi/scsipi_all.h> 34 #include <dev/scsipi/scsiconf.h> 35 #include <dev/scsipi/scsi_message.h> 36 37 #include <pmax/ibus/siireg.h> 38 #include <pmax/ibus/siivar.h> 39 40 #include <pmax/ibus/ibusvar.h> /* interrupt etablish */ 41 42 #include <pmax/pmax/kn01.h> /* kn01 (ds3100) address constants */ 43 #include <pmax/pmax/pmaxtype.h> 44 45 46 static void kn230_copytobuf(u_short *src, /* NB: must be short aligned */ 47 volatile u_short *dst, int length); 48 static void kn230_copyfrombuf(volatile u_short *src, char *dst, 49 int length); 50 51 static void kn01_copytobuf(u_short *src, /* NB: must be short aligned */ 52 volatile u_short *dst, int length); 53 static void kn01_copyfrombuf(volatile u_short *src, char *dst, 54 int length); 55 56 /* 57 * Autoconfig definition of driver front-end 58 */ 59 static int sii_ds_match(device_t, struct cfdata *, void *); 60 static void sii_ds_attach(device_t, device_t, void *); 61 62 CFATTACH_DECL_NEW(sii_ds, sizeof(struct siisoftc), 63 sii_ds_match, sii_ds_attach, NULL, NULL); 64 65 /* define a safe address in the SCSI buffer for doing status & message DMA */ 66 #define SII_BUF_ADDR (MIPS_PHYS_TO_KSEG1(KN01_SYS_SII_B_START) \ 67 + SII_MAX_DMA_XFER_LENGTH * 14) 68 69 /* 70 * Match driver on Decstation (2100, 3100, 5100) based on name and probe. 71 */ 72 static int 73 sii_ds_match(device_t parent, cfdata_t cf, void *aux) 74 { 75 struct ibus_attach_args *ia = aux; 76 void *siiaddr; 77 78 if (strcmp(ia->ia_name, "sii") != 0) 79 return (0); 80 siiaddr = (void *)ia->ia_addr; 81 if (badaddr(siiaddr, 4)) 82 return (0); 83 return (1); 84 } 85 86 static void 87 sii_ds_attach(device_t parent, device_t self, void *aux) 88 { 89 struct ibus_attach_args *ia = aux; 90 struct siisoftc *sc = device_private(self); 91 92 sc->sc_dev = self; 93 sc->sc_regs = (SIIRegs *)MIPS_PHYS_TO_KSEG1(ia->ia_addr); 94 95 /* set up scsi buffer. XXX Why statically allocated? */ 96 sc->sc_buf = (void*)(MIPS_PHYS_TO_KSEG1(KN01_SYS_SII_B_START)); 97 98 if (systype == DS_PMAX) { 99 #if 0 100 sc->sii_copytobuf = CopyToBuffer; 101 sc->sii_copyfrombuf = CopyFromBuffer; 102 #else 103 sc->sii_copytobuf = kn01_copytobuf; 104 sc->sii_copyfrombuf = kn01_copyfrombuf; 105 #endif 106 } else { 107 sc->sii_copytobuf = kn230_copytobuf; 108 sc->sii_copyfrombuf = kn230_copyfrombuf; 109 } 110 111 /* Do the common parts of attachment. */ 112 sc->sc_adapter.adapt_request = sii_scsi_request; 113 sc->sc_adapter.adapt_minphys = minphys; 114 siiattach(sc); 115 116 /* tie pseudo-slot to device */ 117 ibus_intr_establish(parent, (void*)ia->ia_cookie, IPL_BIO, siiintr, sc); 118 } 119 120 121 /* 122 * Padded DMA copy functions 123 * 124 */ 125 126 /* 127 * XXX assumes src is always 32-bit aligned. 128 * currently safe on sii driver, but API and casts should be changed. 129 */ 130 static void 131 kn230_copytobuf(u_short *src, volatile u_short *dst, int len) 132 { 133 u_int *wsrc = (u_int *)src; 134 volatile u_int *wdst = (volatile u_int *)dst; 135 int i, n; 136 137 #if defined(DIAGNOSTIC) || defined(DEBUG) 138 if ((u_int)(src) & 0x3) { 139 printf("kn230: copytobuf, src %p misaligned\n", src); 140 } 141 if ((u_int)(dst) & 0x3) { 142 printf("kn230: copytobuf, dst %p misaligned\n", dst); 143 } 144 #endif 145 146 /* DMA buffer is allocated in 32-bit words, so just copy words. */ 147 n = len / 4; 148 if (len & 0x3) 149 n++; 150 for (i = 0; i < n; i++) { 151 *wdst = *wsrc; 152 wsrc++; 153 wdst+= 2; 154 } 155 156 wbflush(); /* XXX not necessary? */ 157 } 158 159 160 /* 161 * XXX assumes dst is always 32-bit aligned. 162 * currently safe on sii driver, but API and casts should be changed. 163 */ 164 static void 165 kn230_copyfrombuf(volatile u_short *src, char *dst, int len) 166 /* dst: XXX assume 32-bit aligned? */ 167 { 168 volatile u_int *wsrc = (volatile u_int *)src; 169 u_int *wdst = (u_int *)dst; 170 int i, n; 171 172 #if defined(DIAGNOSTIC) || defined(DEBUG) 173 if ((u_int)(src) & 0x3) { 174 printf("kn230: copyfrombuf, src %p misaligned\n", src); 175 } 176 if ((u_int)(dst) & 0x3) { 177 printf("kn230: copyfrombuf, dst %p misaligned\n", dst); 178 } 179 #endif 180 181 n = len / 4; 182 183 for (i = 0; i < n; i++) { 184 *wdst = *wsrc; 185 wsrc += 2; 186 wdst++; 187 } 188 189 if (len & 0x3) { 190 u_int lastword = *wsrc; 191 192 if (len & 0x2) 193 *((u_short*)(wdst)) = (u_short) (lastword); 194 195 if (len & 0x1) 196 ((u_char*)(wdst))[2] = (u_char) (lastword >> 16); 197 } 198 199 wbflush(); /* XXX not necessary? */ 200 } 201 202 203 static void 204 kn01_copytobuf(u_short *src, volatile u_short *dst, int len) 205 { 206 #if defined(DIAGNOSTIC) || defined(DEBUG) 207 if ((u_int)(src) & 0x3) { 208 printf("kn01: copytobuf, src %p misaligned\n", src); 209 } 210 if ((u_int)(dst) & 0x3) { 211 printf("kn01: copytobuf, dst %p misaligned\n", dst); 212 } 213 #endif 214 215 CopyToBuffer(src, dst, len); 216 } 217 218 static void 219 kn01_copyfrombuf(volatile u_short *src, char *dst, int len) 220 /* dst: XXX assume 32-bit aligned? */ 221 { 222 #if defined(DIAGNOSTIC) || defined(DEBUG) 223 if ((u_int)(src) & 0x3) { 224 printf("kn01: copyfrombuf, src %p misaligned\n", src); 225 } 226 if ((u_int)(dst) & 0x3) { 227 printf("kn01: copyfrombuf, dst %p misaligned\n", dst); 228 } 229 230 #endif 231 CopyFromBuffer(src, dst, len); 232 233 } 234