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