1/*	$NetBSD: ahc_isa.c,v 1.42 2019/11/10 21:16:28 chs Exp $	*/
2
3/*
4 * Product specific probe and attach routines for:
5 * 	AHA-284X VL-bus SCSI controllers
6 *
7 * Copyright (c) 1994, 1995, 1996, 1997, 1998 Justin T. Gibbs.
8 * All rights reserved.
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 immediately at the beginning of the file, without modification,
15 *    this list of conditions, and the following disclaimer.
16 * 2. The name of the author may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * $FreeBSD: src/sys/dev/aic7xxx/ahc_eisa.c,v 1.15 2000/01/29 14:22:19 peter Exp $
32 */
33
34/*-
35 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
36 * All rights reserved.
37 *
38 * This code is derived from software contributed to The NetBSD Foundation
39 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
40 * NASA Ames Research Center.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 *    notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 *    notice, this list of conditions and the following disclaimer in the
49 *    documentation and/or other materials provided with the distribution.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
52 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
53 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
54 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
55 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
56 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
57 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
58 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
59 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61 * POSSIBILITY OF SUCH DAMAGE.
62 */
63
64/*
65 * Copyright (c) 1995, 1996 Christopher G. Demetriou
66 * All rights reserved.
67 *
68 * Redistribution and use in source and binary forms, with or without
69 * modification, are permitted provided that the following conditions
70 * are met:
71 * 1. Redistributions of source code must retain the above copyright
72 *    notice, this list of conditions and the following disclaimer.
73 * 2. Redistributions in binary form must reproduce the above copyright
74 *    notice, this list of conditions and the following disclaimer in the
75 *    documentation and/or other materials provided with the distribution.
76 * 3. All advertising materials mentioning features or use of this software
77 *    must display the following acknowledgement:
78 *      This product includes software developed by Christopher G. Demetriou
79 *      for the NetBSD Project.
80 * 4. The name of the author may not be used to endorse or promote products
81 *    derived from this software without specific prior written permission
82 *
83 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
84 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
85 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
86 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
87 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
88 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
89 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
90 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
91 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
92 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
93 */
94
95/*
96 * This front-end driver is really sort of a hack.  The AHA-284X likes
97 * to masquerade as an EISA device.  However, on VLbus machines with
98 * no EISA signature in the BIOS, the EISA bus will never be scanned.
99 * This is intended to catch the 284X controllers on those systems
100 * by looking in "EISA i/o space" for 284X controllers.
101 *
102 * This relies heavily on i/o port accounting.  We also just use the
103 * EISA macros for everything ... it's a real waste to redefine them.
104 *
105 * Note: there isn't any #ifdef for FreeBSD in this file, since the
106 * FreeBSD EISA driver handles all cases of the 284X.
107 *
108 *	-- Jason R. Thorpe <thorpej@NetBSD.org>
109 *	   July 12, 1996
110 */
111
112#include <sys/cdefs.h>
113__KERNEL_RCSID(0, "$NetBSD: ahc_isa.c,v 1.42 2019/11/10 21:16:28 chs Exp $");
114
115#include <sys/param.h>
116#include <sys/systm.h>
117#include <sys/kernel.h>
118#include <sys/device.h>
119#include <sys/queue.h>
120#include <sys/malloc.h>
121#include <sys/reboot.h>
122
123#include <sys/bus.h>
124#include <machine/intr.h>
125
126#include <dev/scsipi/scsi_all.h>
127#include <dev/scsipi/scsipi_all.h>
128#include <dev/scsipi/scsiconf.h>
129
130#include <dev/isa/isavar.h>
131
132#include <dev/eisa/eisareg.h>
133#include <dev/eisa/eisavar.h>
134#include <dev/eisa/eisadevs.h>
135
136#include <dev/ic/aic7xxx_osm.h>
137#include <dev/ic/aic7xxx_inline.h>
138#include <dev/ic/aic77xxreg.h>
139#include <dev/ic/aic77xxvar.h>
140#include <dev/ic/smc93cx6var.h>
141
142/* IO port address setting range as EISA slot number */
143#define AHC_ISA_MIN_SLOT	0x1	/* from iobase = 0x1c00 */
144#define AHC_ISA_MAX_SLOT	0xe	/* to   iobase = 0xec00 */
145
146#define AHC_ISA_SLOT_OFFSET	AHC_EISA_SLOT_OFFSET
147#define AHC_ISA_IOSIZE		AHC_EISA_IOSIZE
148
149/*
150 * I/O port offsets
151 */
152#define	AHC_ISA_VID		(EISA_SLOTOFF_VID - AHC_ISA_SLOT_OFFSET)
153#define	AHC_ISA_PID		(EISA_SLOTOFF_PID - AHC_ISA_SLOT_OFFSET)
154#define	AHC_ISA_PRIMING		AHC_ISA_VID	/* enable vendor/product ID */
155
156/*
157 * AHC_ISA_PRIMING register values (write)
158 */
159#define	AHC_ISA_PRIMING_VID(index)	(AHC_ISA_VID + (index))
160#define	AHC_ISA_PRIMING_PID(index)	(AHC_ISA_PID + (index))
161
162int	ahc_isa_idstring(bus_space_tag_t, bus_space_handle_t, char *);
163int	ahc_isa_match(struct isa_attach_args *, bus_addr_t);
164
165int	ahc_isa_probe(device_t, cfdata_t, void *);
166void	ahc_isa_attach(device_t, device_t, void *);
167void	aha2840_load_seeprom(struct ahc_softc *ahc);
168static int verify_seeprom_cksum(struct seeprom_config *sc);
169
170CFATTACH_DECL_NEW(ahc_isa, sizeof(struct ahc_softc),
171    ahc_isa_probe, ahc_isa_attach, NULL, NULL);
172
173/*
174 * This keeps track of which slots are to be checked next if the
175 * iobase locator is a wildcard.  A simple static variable isn't enough,
176 * since it's conceivable that a system might have more than one ISA
177 * bus.
178 *
179 * The "bus" member is the unit number of the parent ISA bus, e.g. "0"
180 * for "isa0".
181 */
182struct ahc_isa_slot {
183	LIST_ENTRY(ahc_isa_slot)	link;
184	int				bus;
185	int				slot;
186};
187static LIST_HEAD(, ahc_isa_slot) ahc_isa_all_slots;
188static int ahc_isa_slot_initialized;
189
190int
191ahc_isa_idstring(bus_space_tag_t iot, bus_space_handle_t ioh, char *idstring)
192{
193	uint8_t vid[EISA_NVIDREGS], pid[EISA_NPIDREGS];
194	int i;
195
196	/* Get the vendor ID bytes */
197	for (i = 0; i < EISA_NVIDREGS; i++) {
198		bus_space_write_1(iot, ioh, AHC_ISA_PRIMING,
199		    AHC_ISA_PRIMING_VID(i));
200		vid[i] = bus_space_read_1(iot, ioh, AHC_ISA_VID + i);
201	}
202
203	/* Check for device existence */
204	if (EISA_VENDID_NODEV(vid)) {
205#if 0
206		aprint_error("ahc_isa_idstring: no device at 0x%lx\n",
207		    ioh); /* XXX knows about ioh guts */
208		aprint_error("\t(0x%x, 0x%x)\n", vid[0], vid[1]);
209#endif
210		return (0);
211	}
212
213	/* And check that the firmware didn't biff something badly */
214	if (EISA_VENDID_IDDELAY(vid)) {
215		aprint_error("ahc_isa_idstring: BIOS biffed it at 0x%lx\n",
216		    ioh);	/* XXX knows about ioh guts */
217		return (0);
218	}
219
220	/* Get the product ID bytes */
221	for (i = 0; i < EISA_NPIDREGS; i++) {
222		bus_space_write_1(iot, ioh, AHC_ISA_PRIMING,
223		    AHC_ISA_PRIMING_PID(i));
224		pid[i] = bus_space_read_1(iot, ioh, AHC_ISA_PID + i);
225	}
226
227	/* Create the ID string from the vendor and product IDs */
228	idstring[0] = EISA_VENDID_0(vid);
229	idstring[1] = EISA_VENDID_1(vid);
230	idstring[2] = EISA_VENDID_2(vid);
231	idstring[3] = EISA_PRODID_0(pid);
232	idstring[4] = EISA_PRODID_1(pid);
233	idstring[5] = EISA_PRODID_2(pid);
234	idstring[6] = EISA_PRODID_3(pid);
235	idstring[7] = '\0';		/* sanity */
236
237	return (1);
238}
239
240int
241ahc_isa_match(struct isa_attach_args *ia, bus_addr_t iobase)
242{
243	bus_space_tag_t iot = ia->ia_iot;
244	bus_space_handle_t ioh;
245	int irq;
246	char idstring[EISA_IDSTRINGLEN];
247
248	/*
249	 * Get a mapping for the while slot-specific address
250	 * space.  If we can't, assume nothing's there, but
251	 * warn about it.
252	 */
253	if (bus_space_map(iot, iobase, AHC_ISA_IOSIZE, 0, &ioh)) {
254#if 0
255		/*
256		 * Don't print anything out here, since this could
257		 * be common on machines configured to look for
258		 * ahc_eisa and ahc_isa.
259		 */
260		aprint_error("ahc_isa_match: can't map I/O space for 0x%x\n",
261		    iobase);
262#endif
263		return (0);
264	}
265
266	if (!ahc_isa_idstring(iot, ioh, idstring))
267		irq = -1;	/* cannot get the ID string */
268	else if (strcmp(idstring, "ADP7756") &&
269	    strcmp(idstring, "ADP7757"))
270		irq = -1;	/* unknown ID strings */
271	else
272		irq = ahc_aic77xx_irq(iot, ioh);
273
274	bus_space_unmap(iot, ioh, AHC_ISA_IOSIZE);
275
276	if (irq < 0)
277		return (0);
278
279	if (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ &&
280	    ia->ia_irq[0].ir_irq != irq) {
281		aprint_error("ahc_isa_match: irq mismatch (kernel %d, card %d)\n",
282		    ia->ia_irq[0].ir_irq, irq);
283		return (0);
284	}
285
286	/* We have a match */
287	ia->ia_nio = 1;
288	ia->ia_io[0].ir_addr = iobase;
289	ia->ia_io[0].ir_size = AHC_ISA_IOSIZE;
290
291	ia->ia_nirq = 1;
292	ia->ia_irq[0].ir_irq = irq;
293
294	ia->ia_ndrq = 0;
295	ia->ia_niomem = 0;
296
297	return (1);
298}
299
300/*
301 * Check the slots looking for a board we recognise
302 * If we find one, note its address (slot) and call
303 * the actual probe routine to check it out.
304 */
305int
306ahc_isa_probe(device_t parent, cfdata_t match, void *aux)
307{
308	struct isa_attach_args *ia = aux;
309	struct ahc_isa_slot *as;
310
311	if (ahc_isa_slot_initialized == 0) {
312		LIST_INIT(&ahc_isa_all_slots);
313		ahc_isa_slot_initialized = 1;
314	}
315
316	if (ia->ia_nio < 1)
317		return (0);
318	if (ia->ia_nirq < 1)
319		return (0);
320
321	if (ISA_DIRECT_CONFIG(ia))
322		return (0);
323
324	if (ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT)
325		return (ahc_isa_match(ia, ia->ia_io[0].ir_addr));
326
327	/*
328	 * Find this bus's state.  If we don't yet have a slot
329	 * marker, allocate and initialize one.
330	 */
331	for (as = ahc_isa_all_slots.lh_first; as != NULL;
332	    as = as->link.le_next)
333		if (as->bus == device_unit(parent))
334			goto found_slot_marker;
335
336	/*
337	 * Don't have one, so make one.
338	 */
339	as = malloc(sizeof(struct ahc_isa_slot), M_DEVBUF, M_WAITOK);
340	as->bus = device_unit(parent);
341	as->slot = AHC_ISA_MIN_SLOT;
342	LIST_INSERT_HEAD(&ahc_isa_all_slots, as, link);
343
344 found_slot_marker:
345
346	for (; as->slot <= AHC_ISA_MAX_SLOT; as->slot++) {
347		if (ahc_isa_match(ia, EISA_SLOT_ADDR(as->slot) +
348		    AHC_ISA_SLOT_OFFSET)) {
349			as->slot++; /* next slot to search */
350			return (1);
351		}
352	}
353
354	/* No matching cards were found. */
355	return (0);
356}
357
358void
359ahc_isa_attach(device_t parent, device_t self, void *aux)
360{
361	struct ahc_softc *ahc = device_private(self);
362	struct isa_attach_args *ia = aux;
363	bus_space_tag_t iot = ia->ia_iot;
364	bus_space_handle_t ioh;
365	int irq, intrtype;
366	const char *intrtypestr;
367	char idstring[EISA_IDSTRINGLEN];
368	u_char intdef;
369
370	ahc->sc_dev = self;
371	aprint_naive(": SCSI controller\n");
372
373	if (bus_space_map(iot, ia->ia_io[0].ir_addr, ia->ia_io[0].ir_size,
374	    0, &ioh)) {
375		aprint_error(": can't map i/o space\n");
376		return;
377	}
378	if (!ahc_isa_idstring(iot, ioh, idstring)) {
379		aprint_error(": can't read ID string\n");
380		goto free_io;
381	}
382	if ((irq = ahc_aic77xx_irq(iot, ioh)) < 0) {
383		aprint_error(": ahc_aic77xx_irq failed\n");
384		goto free_io;
385	}
386
387	if (strcmp(idstring, "ADP7756") == 0) {
388		aprint_normal(": %s\n", EISA_PRODUCT_ADP7756);
389	} else if (strcmp(idstring, "ADP7757") == 0) {
390		aprint_normal(": %s\n", EISA_PRODUCT_ADP7757);
391	} else {
392		aprint_error(": unknown device type %s\n", idstring);
393		goto free_io;
394	}
395
396	/*
397	 * Tell the bus-DMA interface that we can do 32bit DMA
398	 * NOTE: this variable is first referenced in ahc_init().
399	 */
400	ahc->sc_dmaflags = ISABUS_DMA_32BIT;
401
402	ahc_set_name(ahc, device_xname(ahc->sc_dev));
403	ahc->parent_dmat = ia->ia_dmat;
404	ahc->channel = 'A';
405	ahc->chip =  AHC_AIC7770|AHC_VL;
406	ahc->features = AHC_AIC7770_FE;
407	ahc->bugs |= AHC_TMODE_WIDEODD_BUG;
408	ahc->flags |= AHC_PAGESCBS;
409	ahc->tag = iot;
410	ahc->bsh = ioh;
411
412	if (ahc_softc_init(ahc) != 0)
413		goto free_io;
414
415	ahc_intr_enable(ahc, false);
416
417	if (ahc_reset(ahc) != 0)
418		goto free_io;
419
420	intdef = bus_space_read_1(iot, ioh, INTDEF);
421
422	if (intdef & EDGE_TRIG) {
423		intrtype = IST_EDGE;
424		intrtypestr = "edge triggered";
425	} else {
426		intrtype = IST_LEVEL;
427		intrtypestr = "level sensitive";
428	}
429	ahc->ih = isa_intr_establish(ia->ia_ic, irq,
430	    intrtype, IPL_BIO, ahc_intr, ahc);
431	if (ahc->ih == NULL) {
432		aprint_error_dev(ahc->sc_dev,
433		    "couldn't establish %s interrupt\n", intrtypestr);
434		goto free_io;
435	}
436
437	/*
438	 * Tell the user what type of interrupts we're using.
439	 * useful for debugging irq problems
440	 */
441	if (bootverbose) {
442		aprint_verbose_dev(ahc->sc_dev, "Using %s interrupts\n",
443		       intrtypestr);
444	}
445
446	/*
447	 * Now that we know we own the resources we need, do the
448	 * card initialization.
449	 */
450	aha2840_load_seeprom(ahc);
451
452	/* Attach sub-devices */
453	if (ahc_aic77xx_attach(ahc) == 0)
454		return; /* succeed */
455
456	/* failed */
457	isa_intr_disestablish(ia->ia_ic, ahc->ih);
458free_io:
459	bus_space_unmap(iot, ioh, ia->ia_io[0].ir_size);
460}
461
462/*
463 * Read the 284x SEEPROM.
464 */
465void
466aha2840_load_seeprom(struct ahc_softc *ahc)
467{
468	struct	  seeprom_descriptor sd;
469	struct	  seeprom_config sc;
470	uint8_t  scsi_conf;
471	int	  have_seeprom;
472
473	sd.sd_tag = ahc->tag;
474	sd.sd_bsh = ahc->bsh;
475	sd.sd_control_offset = SEECTL_2840;
476	sd.sd_status_offset = STATUS_2840;
477	sd.sd_dataout_offset = STATUS_2840;
478	sd.sd_chip = C46;
479	sd.sd_MS = 0;
480	sd.sd_RDY = EEPROM_TF;
481	sd.sd_CS = CS_2840;
482	sd.sd_CK = CK_2840;
483	sd.sd_DO = DO_2840;
484	sd.sd_DI = DI_2840;
485
486	if (bootverbose)
487		aprint_verbose("%s: Reading SEEPROM...", ahc_name(ahc));
488	have_seeprom = read_seeprom(&sd, (uint16_t *)&sc,
489				    /*start_addr*/0, sizeof(sc)/2);
490
491	if (have_seeprom) {
492		if (verify_seeprom_cksum(&sc) == 0) {
493			if(bootverbose)
494				aprint_verbose ("checksum error\n");
495			have_seeprom = 0;
496		} else if (bootverbose) {
497			aprint_verbose("done.\n");
498		}
499	}
500
501	if (!have_seeprom) {
502		if (bootverbose)
503			aprint_verbose("%s: No SEEPROM available\n",
504			    ahc_name(ahc));
505		ahc->flags |= AHC_USEDEFAULTS;
506	} else {
507		/*
508		 * Put the data we've collected down into SRAM
509		 * where ahc_init will find it.
510		 */
511		int i;
512		int max_targ = (ahc->features & AHC_WIDE) != 0 ? 16 : 8;
513		uint16_t discenable;
514
515		discenable = 0;
516		for (i = 0; i < max_targ; i++){
517	                uint8_t target_settings;
518			target_settings = (sc.device_flags[i] & CFXFER) << 4;
519			if (sc.device_flags[i] & CFSYNCH)
520				target_settings |= SOFS;
521			if (sc.device_flags[i] & CFWIDEB)
522				target_settings |= WIDEXFER;
523			if (sc.device_flags[i] & CFDISC)
524				discenable |= (0x01 << i);
525			ahc_outb(ahc, TARG_SCSIRATE + i, target_settings);
526		}
527		ahc_outb(ahc, DISC_DSB, ~(discenable & 0xff));
528		ahc_outb(ahc, DISC_DSB + 1, ~((discenable >> 8) & 0xff));
529
530		ahc->our_id = sc.brtime_id & CFSCSIID;
531
532		scsi_conf = (ahc->our_id & 0x7);
533		if (sc.adapter_control & CFSPARITY)
534			scsi_conf |= ENSPCHK;
535		if (sc.adapter_control & CFRESETB)
536			scsi_conf |= RESET_SCSI;
537
538		if (sc.bios_control & CF284XEXTEND)
539			ahc->flags |= AHC_EXTENDED_TRANS_A;
540		/* Set SCSICONF info */
541		ahc_outb(ahc, SCSICONF, scsi_conf);
542
543		if (sc.adapter_control & CF284XSTERM)
544			ahc->flags |= AHC_TERM_ENB_A;
545	}
546}
547
548static int
549verify_seeprom_cksum(struct seeprom_config *sc)
550{
551	int i;
552	int maxaddr;
553	uint32_t checksum;
554	uint16_t *scarray;
555
556	maxaddr = (sizeof(*sc)/2) - 1;
557	checksum = 0;
558	scarray = (uint16_t *)sc;
559
560	for (i = 0; i < maxaddr; i++)
561		checksum = checksum + scarray[i];
562	if (checksum == 0
563	 || (checksum & 0xFFFF) != sc->checksum) {
564		return (0);
565	} else {
566		return(1);
567	}
568}
569