1 /* $NetBSD: csc.c,v 1.21 2021/08/07 16:18:40 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Scott Stevens.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Cumana SCSI-2 driver uses the SFAS216 generic driver
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: csc.c,v 1.21 2021/08/07 16:18:40 thorpej Exp $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43
44 #include <uvm/uvm_extern.h>
45
46 #include <dev/scsipi/scsi_all.h>
47 #include <dev/scsipi/scsipi_all.h>
48 #include <dev/scsipi/scsiconf.h>
49 #include <machine/io.h>
50 #include <machine/intr.h>
51 #include <machine/bootconfig.h>
52 #include <acorn32/podulebus/podulebus.h>
53 #include <acorn32/podulebus/sfasreg.h>
54 #include <acorn32/podulebus/sfasvar.h>
55 #include <acorn32/podulebus/cscreg.h>
56 #include <acorn32/podulebus/cscvar.h>
57 #include <dev/podulebus/podules.h>
58 #include <dev/podulebus/powerromreg.h>
59
60 int cscmatch(device_t, cfdata_t, void *);
61 void cscattach(device_t, device_t, void *);
62
63 CFATTACH_DECL_NEW(csc, sizeof(struct csc_softc),
64 cscmatch, cscattach, NULL, NULL);
65
66 int csc_intr(void *);
67 int csc_setup_dma(void *, void *, int, int);
68 int csc_build_dma_chain(void *, void *, void *, int);
69 int csc_need_bump(void *, void *, int);
70 void csc_led(void *, int);
71
72 void csc_set_dma_adr(struct sfas_softc *, void *);
73 void csc_set_dma_tc(struct sfas_softc *, unsigned int);
74 void csc_set_dma_mode(struct sfas_softc *, int);
75
76 /*
77 * if we are a Cumana SCSI-2 card
78 */
79 int
cscmatch(device_t parent,cfdata_t cf,void * aux)80 cscmatch(device_t parent, cfdata_t cf, void *aux)
81 {
82 struct podule_attach_args *pa = aux;
83
84 /* Look for the card */
85 if (pa->pa_product == PODULE_CUMANA_SCSI2)
86 return 1;
87
88 /* PowerROM */
89 if (pa->pa_product == PODULE_ALSYSTEMS_SCSI &&
90 podulebus_initloader(pa) == 0 &&
91 podloader_callloader(pa, 0, 0) == PRID_CUMANA_SCSI2)
92 return 1;
93
94 return 0;
95 }
96
97 void
cscattach(device_t parent,device_t self,void * aux)98 cscattach(device_t parent, device_t self, void *aux)
99 {
100 struct csc_softc *sc = device_private(self);
101 struct podule_attach_args *pa;
102 csc_regmap_p rp = &sc->sc_regmap;
103 vu_char *fas;
104 int loop;
105
106 pa = aux;
107
108 if (pa->pa_podule_number == -1)
109 panic("Podule has disappeared !");
110
111 sc->sc_specific.sc_podule_number = pa->pa_podule_number;
112 sc->sc_specific.sc_podule = pa->pa_podule;
113 sc->sc_specific.sc_iobase =
114 (vu_char *)sc->sc_specific.sc_podule->mod_base;
115
116 rp->status0 = &sc->sc_specific.sc_iobase[CSC_STATUS0];
117 rp->alatch = &sc->sc_specific.sc_iobase[CSC_ALATCH];
118 rp->dack = (vu_short *)&sc->sc_specific.sc_iobase[CSC_DACK];
119 fas = &sc->sc_specific.sc_iobase[CSC_FAS_OFFSET_BASE];
120
121 rp->FAS216.sfas_tc_low = &fas[CSC_FAS_OFFSET_TCL];
122 rp->FAS216.sfas_tc_mid = &fas[CSC_FAS_OFFSET_TCM];
123 rp->FAS216.sfas_fifo = &fas[CSC_FAS_OFFSET_FIFO];
124 rp->FAS216.sfas_command = &fas[CSC_FAS_OFFSET_COMMAND];
125 rp->FAS216.sfas_dest_id = &fas[CSC_FAS_OFFSET_DESTID];
126 rp->FAS216.sfas_timeout = &fas[CSC_FAS_OFFSET_TIMEOUT];
127 rp->FAS216.sfas_syncper = &fas[CSC_FAS_OFFSET_PERIOD];
128 rp->FAS216.sfas_syncoff = &fas[CSC_FAS_OFFSET_OFFSET];
129 rp->FAS216.sfas_config1 = &fas[CSC_FAS_OFFSET_CONFIG1];
130 rp->FAS216.sfas_clkconv = &fas[CSC_FAS_OFFSET_CLKCONV];
131 rp->FAS216.sfas_test = &fas[CSC_FAS_OFFSET_TEST];
132 rp->FAS216.sfas_config2 = &fas[CSC_FAS_OFFSET_CONFIG2];
133 rp->FAS216.sfas_config3 = &fas[CSC_FAS_OFFSET_CONFIG3];
134 rp->FAS216.sfas_tc_high = &fas[CSC_FAS_OFFSET_TCH];
135 rp->FAS216.sfas_fifo_bot = &fas[CSC_FAS_OFFSET_FIFOBOT];
136
137 sc->sc_softc.sc_dev = self;
138 sc->sc_softc.sc_fas = (sfas_regmap_p)rp;
139 sc->sc_softc.sc_spec = &sc->sc_specific;
140
141 sc->sc_softc.sc_led = csc_led;
142
143 sc->sc_softc.sc_setup_dma = csc_setup_dma;
144 sc->sc_softc.sc_build_dma_chain = csc_build_dma_chain;
145 sc->sc_softc.sc_need_bump = csc_need_bump;
146
147 sc->sc_softc.sc_clock_freq = 8; /* Cumana runs at 8MHz */
148 sc->sc_softc.sc_timeout = 250; /* Set default timeout to 250ms */
149 sc->sc_softc.sc_config_flags = SFAS_NO_DMA /*| SFAS_NF_DEBUG*/;
150 sc->sc_softc.sc_host_id = 7; /* Should check the jumpers */
151
152 sc->sc_softc.sc_bump_sz = PAGE_SIZE;
153 sc->sc_softc.sc_bump_pa = 0x0;
154
155 sfasinitialize((struct sfas_softc *)sc);
156
157 sc->sc_softc.sc_adapter.adapt_dev = self;
158 sc->sc_softc.sc_adapter.adapt_nchannels = 1;
159 sc->sc_softc.sc_adapter.adapt_openings = 7;
160 sc->sc_softc.sc_adapter.adapt_max_periph = 1;
161 sc->sc_softc.sc_adapter.adapt_ioctl = NULL;
162 sc->sc_softc.sc_adapter.adapt_minphys = sfas_minphys;
163 sc->sc_softc.sc_adapter.adapt_request = sfas_scsi_request;
164
165 sc->sc_softc.sc_channel.chan_adapter = &sc->sc_softc.sc_adapter;
166 sc->sc_softc.sc_channel.chan_bustype = &scsi_bustype;
167 sc->sc_softc.sc_channel.chan_channel = 0;
168 sc->sc_softc.sc_channel.chan_ntargets = 8;
169 sc->sc_softc.sc_channel.chan_nluns = 8;
170 sc->sc_softc.sc_channel.chan_id = sc->sc_softc.sc_host_id;
171
172 /* Provide an override for the host id */
173 (void)get_bootconf_option(boot_args, "csc.hostid",
174 BOOTOPT_TYPE_INT, &sc->sc_softc.sc_channel.chan_id);
175
176 printf(": host=%d", sc->sc_softc.sc_channel.chan_id);
177
178 /* initialise the alatch */
179 sc->sc_specific.sc_alatch_defs = (CSC_POLL?0:CSC_ALATCH_DEFS_INTEN);
180 for (loop = 0; loop < 8; loop ++) {
181 if(loop != 3)
182 *rp->alatch = (loop << 1) |
183 ((sc->sc_specific.sc_alatch_defs & (1 << loop))?1:0);
184 }
185
186 #if CSC_POLL == 0
187 evcnt_attach_dynamic(&sc->sc_softc.sc_intrcnt, EVCNT_TYPE_INTR, NULL,
188 device_xname(self), "intr");
189 sc->sc_softc.sc_ih = podulebus_irq_establish(pa->pa_ih, IPL_BIO,
190 csc_intr, &sc->sc_softc, &sc->sc_softc.sc_intrcnt);
191 if (sc->sc_softc.sc_ih == NULL)
192 panic("%s: Cannot install IRQ handler", device_xname(self));
193 #else
194 printf(" polling");
195 sc->sc_softc.sc_adapter.adapt_flags |= SCSIPI_ADAPT_POLL_ONLY;
196 #endif
197 printf("\n");
198
199 /* attach all scsi units on us */
200 config_found(self, &sc->sc_softc.sc_channel, scsiprint, CFARGS_NONE);
201 }
202
203
204 int
csc_intr(void * arg)205 csc_intr(void *arg)
206 {
207 struct sfas_softc *dev = arg;
208 csc_regmap_p rp;
209 int quickints;
210
211 rp = (csc_regmap_p)dev->sc_fas;
212
213 if (*rp->FAS216.sfas_status & SFAS_STAT_INTERRUPT_PENDING) {
214 quickints = 16;
215 do {
216 dev->sc_status = *rp->FAS216.sfas_status;
217 dev->sc_interrupt = *rp->FAS216.sfas_interrupt;
218
219 if (dev->sc_interrupt & SFAS_INT_RESELECTED) {
220 dev->sc_resel[0] = *rp->FAS216.sfas_fifo;
221 dev->sc_resel[1] = *rp->FAS216.sfas_fifo;
222 }
223 sfasintr(dev);
224
225 } while((*rp->FAS216.sfas_status & SFAS_STAT_INTERRUPT_PENDING)
226 && --quickints);
227 }
228
229 return(0); /* Pass interrupt on down the chain */
230 }
231
232 /* Load transfer address into DMA register */
233 void
csc_set_dma_adr(struct sfas_softc * sc,void * ptr)234 csc_set_dma_adr(struct sfas_softc *sc, void *ptr)
235 {
236 return;
237 }
238
239 /* Set DMA transfer counter */
240 void
csc_set_dma_tc(struct sfas_softc * sc,unsigned int len)241 csc_set_dma_tc(struct sfas_softc *sc, unsigned int len)
242 {
243 *sc->sc_fas->sfas_tc_low = len; len >>= 8;
244 *sc->sc_fas->sfas_tc_mid = len; len >>= 8;
245 *sc->sc_fas->sfas_tc_high = len;
246 }
247
248 /* Set DMA mode */
249 void
csc_set_dma_mode(struct sfas_softc * sc,int mode)250 csc_set_dma_mode(struct sfas_softc *sc, int mode)
251 {
252 }
253
254 /* Initialize DMA for transfer */
255 int
csc_setup_dma(void * sc,void * ptr,int len,int mode)256 csc_setup_dma(void *sc, void *ptr, int len, int mode)
257 {
258
259 return (0);
260 }
261
262 /* Check if address and len is ok for DMA transfer */
263 int
csc_need_bump(void * sc,void * ptr,int len)264 csc_need_bump(void *sc, void *ptr, int len)
265 {
266 int p;
267
268 p = (int)ptr & 0x03;
269
270 if (p) {
271 p = 4-p;
272
273 if (len < 256)
274 p = len;
275 }
276
277 return(p);
278 }
279
280 /* Interrupt driven routines */
281 int
csc_build_dma_chain(void * sc,void * chain,void * p,int l)282 csc_build_dma_chain(void *sc, void *chain, void *p, int l)
283 {
284 return(0);
285 }
286
287 /* Turn on/off led */
288 void
csc_led(void * v,int mode)289 csc_led(void *v, int mode)
290 {
291 struct sfas_softc *sc = v;
292
293 if (mode) {
294 sc->sc_led_status++;
295 } else {
296 if (sc->sc_led_status)
297 sc->sc_led_status--;
298 }
299 }
300