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