sii_ds.c revision 1.5
1/*	$NetBSD: sii_ds.c,v 1.5 2009/03/14 15:36:11 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.5 2009/03/14 15:36:11 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(src, dst, len)
166	volatile u_short *src;
167	char *dst;		/* XXX assume 32-bit aligned? */
168	int len;
169{
170	volatile u_int *wsrc = (volatile u_int *)src;
171	u_int *wdst = (u_int *)dst;
172	int i, n;
173
174#if defined(DIAGNOSTIC) || defined(DEBUG)
175	if ((u_int)(src) & 0x3) {
176		printf("kn230: copyfrombuf, src %p misaligned\n",  src);
177	}
178	if ((u_int)(dst) & 0x3) {
179		printf("kn230: copyfrombuf, dst %p misaligned\n",  dst);
180	}
181#endif
182
183	n = len / 4;
184
185	for (i = 0; i < n; i++) {
186		*wdst = *wsrc;
187		wsrc += 2;
188		wdst++;
189	}
190
191	if (len & 0x3) {
192		u_int lastword = *wsrc;
193
194		if (len & 0x2)
195		    *((u_short*)(wdst)) = (u_short) (lastword);
196
197		if (len & 0x1)
198		    ((u_char*)(wdst))[2] = (u_char) (lastword >> 16);
199	}
200
201	wbflush();		/* XXX not necessary? */
202}
203
204
205static void
206kn01_copytobuf(u_short *src, volatile u_short *dst, int len)
207{
208#if defined(DIAGNOSTIC) || defined(DEBUG)
209	if ((u_int)(src) & 0x3) {
210		printf("kn01: copytobuf, src %p misaligned\n",  src);
211	}
212	if ((u_int)(dst) & 0x3) {
213		printf("kn01: copytobuf, dst %p misaligned\n",  dst);
214	}
215#endif
216
217	CopyToBuffer(src, dst, len);
218}
219
220static void
221kn01_copyfrombuf(src, dst, len)
222	volatile u_short *src;
223	char *dst;		/* XXX assume 32-bit aligned? */
224	int len;
225{
226#if defined(DIAGNOSTIC) || defined(DEBUG)
227	if ((u_int)(src) & 0x3) {
228		printf("kn01: copyfrombuf, src %p misaligned\n",  src);
229	}
230	if ((u_int)(dst) & 0x3) {
231		printf("kn01: copyfrombuf, dst %p misaligned\n",  dst);
232	}
233
234#endif
235	CopyFromBuffer(src, dst, len);
236
237}
238