xref: /netbsd-src/sys/arch/acorn32/eb7500atx/rside.c (revision d7cdbe2716e26606afe53001eb1aadea1d4c1e56)
1 /*	$NetBSD: rside.c,v 1.17 2023/12/20 06:13:59 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 2004 Christopher Gilbert
5  * All rights reserved.
6  *
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 /*
28  * Copyright (c) 1997-1998 Mark Brinicombe
29  * Copyright (c) 1997-1998 Causality Limited
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  * 1. Redistributions of source code must retain the above copyright
35  *    notice, this list of conditions and the following disclaimer.
36  * 2. Redistributions in binary form must reproduce the above copyright
37  *    notice, this list of conditions and the following disclaimer in the
38  *    documentation and/or other materials provided with the distribution.
39  * 3. All advertising materials mentioning features or use of this software
40  *    must display the following acknowledgement:
41  *	This product includes software developed by Mark Brinicombe
42  *	for the NetBSD Project.
43  * 4. The name of the author may not be used to endorse or promote products
44  *    derived from this software without specific prior written permission.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
47  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
50  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
51  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
52  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
53  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
55  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56  */
57 
58 #include <sys/cdefs.h>
59 __KERNEL_RCSID(0, "$NetBSD: rside.c,v 1.17 2023/12/20 06:13:59 thorpej Exp $");
60 
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/conf.h>
64 #include <sys/device.h>
65 #include <sys/bus.h>
66 
67 #include <machine/intr.h>
68 #include <machine/io.h>
69 #include <acorn32/eb7500atx/rsidereg.h>
70 #include <machine/irqhandler.h>
71 
72 #include <dev/ata/atavar.h>
73 #include <dev/ic/wdcvar.h>
74 #include <acorn32/eb7500atx/rsbus.h>
75 
76 /*
77  * RiscStation IDE device.
78  *
79  * This probes and attaches the top level IDE device to the rsbus.
80  * It then configures any children of the IDE device.
81  * The attach args specify whether it is configuring the primary or
82  * secondary channel.
83  * The children are expected to be wdc devices using rside attachments.
84  *
85  * The hardware notes are:
86  * Two ide ports are fitted, each with registers spaced 0x40 bytes apart
87  * with the extra control register at offset 0x380 from the base of the
88  * port.
89  *
90  * Primary:
91  * 	Registers at 0x302b800 (nPCCS1 + 0x0)
92  * 	IRQ connected to nEvent1 (IRQ register D)
93  *
94  * Secondary:
95  * 	Registers at 0x302bc00 (nPCCS1 + 0x400)
96  * 	IRQ connected to nEvent2 (IRQ register D)
97  *
98  * PIO timings can be changed by modifying the access speed register in the
99  * IOMD, as there is nothing else in the nPCCS1 space.
100  *
101  * The Reset line is asserted by unsetting bit 4 in IO register
102  * IOMD + 0x121CC.
103  */
104 
105 /*
106  * RiscStation IDE card softc structure.
107  *
108  * Contains the device node, and global information required by the driver
109  */
110 
111 struct rside_softc {
112 	struct wdc_softc	sc_wdcdev;	/* common wdc definitions */
113 	struct ata_channel	*sc_chanarray[2]; /* channels definition */
114 	struct bus_space        sc_tag;			/* custom tag */
115 	struct rside_channel {
116 		struct ata_channel rc_channel;		/* generic part */
117 		irqhandler_t *rc_ih;			/* irq handler */
118 	} rside_channels[2];
119 	struct wdc_regs sc_wdc_regs[2];
120 };
121 
122 static int	rside_probe	(device_t, cfdata_t, void *);
123 static void	rside_attach	(device_t, device_t, void *);
124 
125 CFATTACH_DECL_NEW(rside, sizeof(struct rside_softc),
126 		rside_probe, rside_attach, NULL, NULL);
127 
128 /*
129  * Create an array of address structures. These define the addresses and
130  * masks needed for the different channels.
131  *
132  * index = channel
133  */
134 
135 const struct {
136 	u_int drive_registers;
137 	u_int aux_register;
138 } rside_info[] = {
139 	{ PRIMARY_DRIVE_REGISTERS_POFFSET, PRIMARY_AUX_REGISTER_POFFSET },
140 	{ SECONDARY_DRIVE_REGISTERS_POFFSET, SECONDARY_AUX_REGISTER_POFFSET }
141 };
142 
143 /*
144  * Card probe function
145  */
146 
147 static int
rside_probe(device_t parent,cfdata_t cf,void * aux)148 rside_probe(device_t parent, cfdata_t cf, void *aux)
149 {
150 	/* if we're including this, then for now assume it exists */
151 	return 1;
152 }
153 
154 /*
155  * Card attach function
156  *
157  */
158 
159 static void
rside_attach(device_t parent,device_t self,void * aux)160 rside_attach(device_t parent, device_t self, void *aux)
161 {
162 	struct rside_softc *sc = device_private(self);
163 	struct rsbus_attach_args *rs = aux;
164 	int channel, i;
165 	struct rside_channel *scp;
166 	struct ata_channel *cp;
167 	struct wdc_regs *wdr;
168 
169 	aprint_normal("\n");
170 
171 	/*
172 	 * we need our own bus tag as the register spacing
173 	 * is not the default.
174 	 *
175 	 * For the rsbus the bus tag cookie is the shift
176 	 * to apply to registers
177 	 * So duplicate the bus space tag and change the
178 	 * cookie.
179 	 */
180 
181 	sc->sc_wdcdev.sc_atac.atac_dev = self;
182 	sc->sc_wdcdev.regs = sc->sc_wdc_regs;
183 	sc->sc_tag = *rs->sa_iot;
184 	sc->sc_tag.bs_cookie = (void *) DRIVE_REGISTER_SPACING_SHIFT;
185 
186 	/* Fill in wdc and channel infos */
187 	sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
188 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
189 	sc->sc_wdcdev.sc_atac.atac_channels = sc->sc_chanarray;
190 	sc->sc_wdcdev.sc_atac.atac_nchannels = 2;
191 	sc->sc_wdcdev.wdc_maxdrives = 2;
192 	for (channel = 0 ; channel < 2; channel++) {
193 		scp = &sc->rside_channels[channel];
194 		sc->sc_chanarray[channel] = &scp->rc_channel;
195 		cp = &scp->rc_channel;
196 		wdr = &sc->sc_wdc_regs[channel];
197 
198 		cp->ch_channel = channel;
199 		cp->ch_atac = &sc->sc_wdcdev.sc_atac;
200 		wdr->cmd_iot = wdr->ctl_iot = &sc->sc_tag;
201 		if (bus_space_map(wdr->cmd_iot,
202 		    rside_info[channel].drive_registers,
203 		    DRIVE_REGISTERS_SPACE, 0, &wdr->cmd_baseioh))
204 			panic("couldn't map drive registers channel = %d,"
205 					"registers@0x08%x\n",
206 					channel,
207 					rside_info[channel].drive_registers);
208 
209 		for (i = 0; i < WDC_NREG; i++) {
210 			if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
211 				i * (DRIVE_REGISTER_BYTE_SPACING >> 2), 4,
212 				&wdr->cmd_iohs[i]) != 0) {
213 				bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh,
214 				    DRIVE_REGISTERS_SPACE);
215 				continue;
216 			}
217 		}
218 		wdc_init_shadow_regs(wdr);
219 
220 		if (bus_space_map(wdr->ctl_iot,
221 		    rside_info[channel].aux_register, 0x4, 0, &wdr->ctl_ioh))
222 		{
223 			bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh,
224 			    DRIVE_REGISTERS_SPACE);
225 			continue;
226 		}
227 
228 		/* attach it to the interrupt */
229 		if ((scp->rc_ih = intr_claim((channel == 0 ? IRQ_NEVENT1 : IRQ_NEVENT2),
230 						IPL_BIO, "rside", wdcintr, cp)) == NULL)
231 			panic("%s: Cannot claim interrupt %d\n",
232 			    device_xname(self),
233 			    (channel == 0 ? IRQ_NEVENT1 : IRQ_NEVENT2));
234 
235 		wdcattach(cp);
236 	}
237 }
238