xref: /openbsd/sys/arch/hppa/gsc/siop_gsc.c (revision a6445c1d)
1 /*	$OpenBSD: siop_gsc.c,v 1.4 2007/08/23 21:01:22 kettenis Exp $	*/
2 
3 /*
4  * Copyright (c) 2007 Mark Kettenis
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <sys/device.h>
21 #include <sys/systm.h>
22 
23 #include <machine/autoconf.h>
24 #include <machine/bus.h>
25 #include <machine/iomod.h>
26 
27 #include <scsi/scsi_all.h>
28 #include <scsi/scsiconf.h>
29 
30 #include <dev/ic/siopreg.h>
31 #include <dev/ic/siopvar_common.h>
32 #include <dev/ic/siopvar.h>
33 
34 #include <hppa/dev/cpudevs.h>
35 #include <hppa/gsc/gscbusvar.h>
36 
37 #define	SIOP_GSC_RESET	0x0000
38 #define	SIOP_GSC_OFFSET	0x0100
39 
40 int siop_gsc_match(struct device *, void *, void *);
41 void siop_gsc_attach(struct device *, struct device *, void *);
42 int siop_gsc_intr(void *);
43 void siop_gsc_reset(struct siop_common_softc *);
44 
45 u_int8_t siop_gsc_r1(void *, bus_space_handle_t, bus_size_t);
46 u_int16_t siop_gsc_r2(void *, bus_space_handle_t, bus_size_t);
47 void siop_gsc_w1(void *, bus_space_handle_t, bus_size_t, u_int8_t);
48 void siop_gsc_w2(void *, bus_space_handle_t, bus_size_t, u_int16_t);
49 
50 struct siop_gsc_softc {
51 	struct siop_softc sc_siop;
52 	bus_space_tag_t sc_iot;
53 	bus_space_handle_t sc_ioh;
54 	struct hppa_bus_space_tag sc_bustag;
55 };
56 
57 struct cfattach siop_gsc_ca = {
58 	sizeof(struct siop_gsc_softc), siop_gsc_match, siop_gsc_attach
59 };
60 
61 int
62 siop_gsc_match(struct device *parent, void *match, void *aux)
63 {
64 	struct gsc_attach_args *ga = aux;
65 
66 	if (ga->ga_type.iodc_type != HPPA_TYPE_FIO ||
67 	    ga->ga_type.iodc_sv_model != HPPA_FIO_FWSCSI)
68 		return 0;
69 
70 	return 1;
71 }
72 
73 void
74 siop_gsc_attach(struct device *parent, struct device *self, void *aux)
75 {
76 	struct siop_gsc_softc *sc = (struct siop_gsc_softc *)self;
77 	struct gsc_attach_args *ga = aux;
78 
79 	sc->sc_iot = ga->ga_iot;
80 	if (bus_space_map(sc->sc_iot, ga->ga_hpa,
81 	    IOMOD_HPASIZE, 0, &sc->sc_ioh)) {
82 		printf(": cannot map io space\n");
83 		return;
84 	}
85 
86 	sc->sc_bustag = *sc->sc_iot;
87 	sc->sc_bustag.hbt_r1 = siop_gsc_r1;
88 	sc->sc_bustag.hbt_r2 = siop_gsc_r2;
89 	sc->sc_bustag.hbt_w1 = siop_gsc_w1;
90 	sc->sc_bustag.hbt_w2 = siop_gsc_w2;
91 
92 	sc->sc_siop.sc_c.features = SF_CHIP_PF | SF_CHIP_BE | SF_BUS_WIDE;
93 	sc->sc_siop.sc_c.maxburst = 4;
94 	sc->sc_siop.sc_c.maxoff = 8;
95 	sc->sc_siop.sc_c.clock_div = 3;
96 	sc->sc_siop.sc_c.clock_period = 250;
97 	sc->sc_siop.sc_c.ram_size = 0;
98 
99 	sc->sc_siop.sc_c.sc_reset = siop_gsc_reset;
100 	sc->sc_siop.sc_c.sc_dmat = ga->ga_dmatag;
101 
102 	sc->sc_siop.sc_c.sc_rt = &sc->sc_bustag;
103 	bus_space_subregion(sc->sc_iot, sc->sc_ioh, SIOP_GSC_OFFSET,
104 	    IOMOD_HPASIZE - SIOP_GSC_OFFSET, &sc->sc_siop.sc_c.sc_rh);
105 
106 	/*
107 	 * Reset the SCSI subsystem.
108 	 */
109 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, SIOP_GSC_RESET, 0);
110 	DELAY(1000);
111 	siop_gsc_reset(&sc->sc_siop.sc_c);
112 
113 	gsc_intr_establish((struct gsc_softc *)parent, ga->ga_irq,
114 	    IPL_BIO, siop_intr, sc, sc->sc_siop.sc_c.sc_dev.dv_xname);
115 
116 	printf(": NCR53C720 rev %d\n", bus_space_read_1(sc->sc_siop.sc_c.sc_rt,
117 	    sc->sc_siop.sc_c.sc_rh, SIOP_CTEST3) >> 4);
118 
119 	siop_attach(&sc->sc_siop);
120 }
121 
122 void
123 siop_gsc_reset(struct siop_common_softc *sc)
124 {
125 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_DCNTL, DCNTL_EA);
126 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST0, CTEST0_EHP);
127 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_CTEST4, CTEST4_MUX);
128 
129 	bus_space_write_1(sc->sc_rt, sc->sc_rh, SIOP_STIME0,
130 	    (0xc << STIME0_SEL_SHIFT));
131 }
132 
133 u_int8_t
134 siop_gsc_r1(void *v, bus_space_handle_t h, bus_size_t o)
135 {
136 	return *(volatile u_int8_t *)(h + (o ^ 3));
137 }
138 
139 u_int16_t
140 siop_gsc_r2(void *v, bus_space_handle_t h, bus_size_t o)
141 {
142 	if (o == SIOP_SIST0) {
143 		u_int16_t reg;
144 
145 		reg = siop_gsc_r1(v, h, SIOP_SIST0);
146 		reg |= siop_gsc_r1(v, h, SIOP_SIST1) << 8;
147 		return reg;
148 	}
149 	return *(volatile u_int16_t *)(h + (o ^ 2));
150 }
151 
152 void
153 siop_gsc_w1(void *v, bus_space_handle_t h, bus_size_t o, u_int8_t vv)
154 {
155 	*(volatile u_int8_t *)(h + (o ^ 3)) = vv;
156 }
157 
158 void
159 siop_gsc_w2(void *v, bus_space_handle_t h, bus_size_t o, u_int16_t vv)
160 {
161 	*(volatile u_int16_t *)(h + (o ^ 2)) = vv;
162 }
163