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