sii_ds.c revision 1.6
1/*	$NetBSD: sii_ds.c,v 1.6 2009/03/14 21:04:14 dsl 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.6 2009/03/14 21:04:14 dsl 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(struct device* parent, struct cfdata *match,
59		    void *aux);
60static void	sii_ds_attach(struct device *parent, struct device *self,
61		    void *aux);
62
63CFATTACH_DECL(sii_ds, sizeof(struct siisoftc),
64    sii_ds_match, sii_ds_attach, NULL, NULL);
65
66/* define a safe address in the SCSI buffer for doing status & message DMA */
67#define SII_BUF_ADDR	(MIPS_PHYS_TO_KSEG1(KN01_SYS_SII_B_START) \
68		+ SII_MAX_DMA_XFER_LENGTH * 14)
69
70/*
71 * Match driver on Decstation (2100, 3100, 5100) based on name and probe.
72 */
73static int
74sii_ds_match(struct device *parent, struct cfdata *match, void *aux)
75{
76	struct ibus_attach_args *ia = aux;
77	void *siiaddr;
78
79	if (strcmp(ia->ia_name, "sii") != 0)
80		return (0);
81	siiaddr = (void *)ia->ia_addr;
82	if (badaddr(siiaddr, 4))
83		return (0);
84	return (1);
85}
86
87static void
88sii_ds_attach(struct device *parent, struct device *self, void *aux)
89{
90	struct ibus_attach_args *ia = aux;
91	struct siisoftc *sc = (struct siisoftc *) self;
92
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 */
130static void
131kn230_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 */
164static void
165kn230_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
203static void
204kn01_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
218static void
219kn01_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