simide.c revision 1.1 1 1.1 reinoud /* $NetBSD: simide.c,v 1.1 2001/10/05 22:27:59 reinoud Exp $ */
2 1.1 reinoud
3 1.1 reinoud /*
4 1.1 reinoud * Copyright (c) 1997-1998 Mark Brinicombe
5 1.1 reinoud * Copyright (c) 1997-1998 Causality Limited
6 1.1 reinoud *
7 1.1 reinoud * Redistribution and use in source and binary forms, with or without
8 1.1 reinoud * modification, are permitted provided that the following conditions
9 1.1 reinoud * are met:
10 1.1 reinoud * 1. Redistributions of source code must retain the above copyright
11 1.1 reinoud * notice, this list of conditions and the following disclaimer.
12 1.1 reinoud * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 reinoud * notice, this list of conditions and the following disclaimer in the
14 1.1 reinoud * documentation and/or other materials provided with the distribution.
15 1.1 reinoud * 3. All advertising materials mentioning features or use of this software
16 1.1 reinoud * must display the following acknowledgement:
17 1.1 reinoud * This product includes software developed by Mark Brinicombe
18 1.1 reinoud * for the NetBSD Project.
19 1.1 reinoud * 4. The name of the author may not be used to endorse or promote products
20 1.1 reinoud * derived from this software without specific prior written permission.
21 1.1 reinoud *
22 1.1 reinoud * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 reinoud * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 reinoud * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 reinoud * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 reinoud * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 reinoud * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 reinoud * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 reinoud * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 reinoud * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 reinoud * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 reinoud *
33 1.1 reinoud * Card driver and probe and attach functions to use generic IDE driver
34 1.1 reinoud * for the Simtec IDE podule
35 1.1 reinoud */
36 1.1 reinoud
37 1.1 reinoud /*
38 1.1 reinoud * Thanks to Gareth Simpson, Simtec Electronics for providing
39 1.1 reinoud * the hardware information
40 1.1 reinoud */
41 1.1 reinoud
42 1.1 reinoud #include <sys/param.h>
43 1.1 reinoud #include <sys/systm.h>
44 1.1 reinoud #include <sys/conf.h>
45 1.1 reinoud #include <sys/device.h>
46 1.1 reinoud #include <sys/malloc.h>
47 1.1 reinoud
48 1.1 reinoud #include <machine/irqhandler.h>
49 1.1 reinoud #include <machine/io.h>
50 1.1 reinoud #include <machine/bus.h>
51 1.1 reinoud #include <acorn32/podulebus/podulebus.h>
52 1.1 reinoud #include <acorn32/podulebus/simidereg.h>
53 1.1 reinoud
54 1.1 reinoud #include <dev/ata/atavar.h>
55 1.1 reinoud #include <dev/ic/wdcvar.h>
56 1.1 reinoud #include <dev/podulebus/podules.h>
57 1.1 reinoud
58 1.1 reinoud
59 1.1 reinoud /*
60 1.1 reinoud * Simtec IDE podule device.
61 1.1 reinoud *
62 1.1 reinoud * This probes and attaches the top level Simtec IDE device to the podulebus.
63 1.1 reinoud * It then configures any children of the Simtec IDE device.
64 1.1 reinoud * The attach args specify whether it is configuring the primary or
65 1.1 reinoud * secondary channel.
66 1.1 reinoud * The children are expected to be wdc devices using simide attachments.
67 1.1 reinoud */
68 1.1 reinoud
69 1.1 reinoud /*
70 1.1 reinoud * Simtec IDE card softc structure.
71 1.1 reinoud *
72 1.1 reinoud * Contains the device node, podule information and global information
73 1.1 reinoud * required by the driver such as the card version and the interrupt mask.
74 1.1 reinoud */
75 1.1 reinoud
76 1.1 reinoud struct simide_softc {
77 1.1 reinoud struct wdc_softc sc_wdcdev; /* common wdc definitions */
78 1.1 reinoud struct channel_softc *wdc_chanarray[2]; /* channels definition */
79 1.1 reinoud podule_t *sc_podule; /* Our podule info */
80 1.1 reinoud int sc_podule_number; /* Our podule number */
81 1.1 reinoud int sc_ctl_reg; /* Global ctl reg */
82 1.1 reinoud int sc_version; /* Card version */
83 1.1 reinoud bus_space_tag_t sc_ctliot; /* Bus tag */
84 1.1 reinoud bus_space_handle_t sc_ctlioh; /* control handle */
85 1.1 reinoud struct bus_space sc_tag; /* custom tag */
86 1.1 reinoud struct simide_channel {
87 1.1 reinoud struct channel_softc wdc_channel; /* generic part */
88 1.1 reinoud irqhandler_t sc_ih; /* interrupt handler */
89 1.1 reinoud int sc_irqmask; /* IRQ mask for this channel */
90 1.1 reinoud } simide_channels[2];
91 1.1 reinoud };
92 1.1 reinoud
93 1.1 reinoud int simide_probe __P((struct device *, struct cfdata *, void *));
94 1.1 reinoud void simide_attach __P((struct device *, struct device *, void *));
95 1.1 reinoud void simide_shutdown __P((void *arg));
96 1.1 reinoud int simide_intr __P((void *arg));
97 1.1 reinoud
98 1.1 reinoud struct cfattach simide_ca = {
99 1.1 reinoud sizeof(struct simide_softc), simide_probe, simide_attach
100 1.1 reinoud };
101 1.1 reinoud
102 1.1 reinoud
103 1.1 reinoud /*
104 1.1 reinoud * Define prototypes for custom bus space functions.
105 1.1 reinoud */
106 1.1 reinoud
107 1.1 reinoud bs_rm_2_proto(simide);
108 1.1 reinoud bs_wm_2_proto(simide);
109 1.1 reinoud
110 1.1 reinoud /*
111 1.1 reinoud * Create an array of address structures. These define the addresses and
112 1.1 reinoud * masks needed for the different channels.
113 1.1 reinoud *
114 1.1 reinoud * index = channel
115 1.1 reinoud */
116 1.1 reinoud
117 1.1 reinoud struct {
118 1.1 reinoud u_int drive_registers;
119 1.1 reinoud u_int aux_register;
120 1.1 reinoud u_int irq_mask;
121 1.1 reinoud } simide_info[] = {
122 1.1 reinoud { PRIMARY_DRIVE_REGISTERS_POFFSET, PRIMARY_AUX_REGISTER_POFFSET,
123 1.1 reinoud CONTROL_PRIMARY_IRQ },
124 1.1 reinoud { SECONDARY_DRIVE_REGISTERS_POFFSET, SECONDARY_AUX_REGISTER_POFFSET,
125 1.1 reinoud CONTROL_SECONDARY_IRQ }
126 1.1 reinoud };
127 1.1 reinoud
128 1.1 reinoud /*
129 1.1 reinoud * Card probe function
130 1.1 reinoud *
131 1.1 reinoud * Just match the manufacturer and podule ID's
132 1.1 reinoud */
133 1.1 reinoud
134 1.1 reinoud int
135 1.1 reinoud simide_probe(parent, cf, aux)
136 1.1 reinoud struct device *parent;
137 1.1 reinoud struct cfdata *cf;
138 1.1 reinoud void *aux;
139 1.1 reinoud {
140 1.1 reinoud struct podule_attach_args *pa = (void *)aux;
141 1.1 reinoud
142 1.1 reinoud if (matchpodule(pa, MANUFACTURER_SIMTEC, PODULE_SIMTEC_IDE, -1) == 0)
143 1.1 reinoud return(0);
144 1.1 reinoud return(1);
145 1.1 reinoud }
146 1.1 reinoud
147 1.1 reinoud /*
148 1.1 reinoud * Card attach function
149 1.1 reinoud *
150 1.1 reinoud * Identify the card version and configure any children.
151 1.1 reinoud * Install a shutdown handler to kill interrupts on shutdown
152 1.1 reinoud */
153 1.1 reinoud
154 1.1 reinoud void
155 1.1 reinoud simide_attach(parent, self, aux)
156 1.1 reinoud struct device *parent, *self;
157 1.1 reinoud void *aux;
158 1.1 reinoud {
159 1.1 reinoud struct simide_softc *sc = (void *)self;
160 1.1 reinoud struct podule_attach_args *pa = (void *)aux;
161 1.1 reinoud int status;
162 1.1 reinoud u_int iobase;
163 1.1 reinoud int channel;
164 1.1 reinoud struct simide_channel *scp;
165 1.1 reinoud struct channel_softc *cp;
166 1.1 reinoud irqhandler_t *ihp;
167 1.1 reinoud
168 1.1 reinoud /* Note the podule number and validate */
169 1.1 reinoud if (pa->pa_podule_number == -1)
170 1.1 reinoud panic("Podule has disappeared !");
171 1.1 reinoud
172 1.1 reinoud sc->sc_podule_number = pa->pa_podule_number;
173 1.1 reinoud sc->sc_podule = pa->pa_podule;
174 1.1 reinoud podules[sc->sc_podule_number].attached = 1;
175 1.1 reinoud
176 1.1 reinoud /*
177 1.1 reinoud * Ok we need our own bus tag as the register spacing
178 1.1 reinoud * is not the default.
179 1.1 reinoud *
180 1.1 reinoud * For the podulebus the bus tag cookie is the shift
181 1.1 reinoud * to apply to registers
182 1.1 reinoud * So duplicate the bus space tag and change the
183 1.1 reinoud * cookie.
184 1.1 reinoud *
185 1.1 reinoud * Also while we are at it replace the default
186 1.1 reinoud * read/write mulitple short functions with
187 1.1 reinoud * optimised versions
188 1.1 reinoud */
189 1.1 reinoud
190 1.1 reinoud sc->sc_tag = *pa->pa_iot;
191 1.1 reinoud sc->sc_tag.bs_cookie = (void *) DRIVE_REGISTER_SPACING_SHIFT;
192 1.1 reinoud sc->sc_tag.bs_rm_2 = simide_bs_rm_2;
193 1.1 reinoud sc->sc_tag.bs_wm_2 = simide_bs_wm_2;
194 1.1 reinoud sc->sc_ctliot = pa->pa_iot;
195 1.1 reinoud
196 1.1 reinoud /* Obtain bus space handles for all the control registers */
197 1.1 reinoud if (bus_space_map(sc->sc_ctliot, pa->pa_podule->mod_base +
198 1.1 reinoud CONTROL_REGISTERS_POFFSET, CONTROL_REGISTER_SPACE, 0,
199 1.1 reinoud &sc->sc_ctlioh))
200 1.1 reinoud panic("%s: Cannot map control registers\n", self->dv_xname);
201 1.1 reinoud
202 1.1 reinoud /* Install a clean up handler to make sure IRQ's are disabled */
203 1.1 reinoud if (shutdownhook_establish(simide_shutdown, (void *)sc) == NULL)
204 1.1 reinoud panic("%s: Cannot install shutdown handler", self->dv_xname);
205 1.1 reinoud
206 1.1 reinoud /* Set the interrupt info for this podule */
207 1.1 reinoud sc->sc_podule->irq_addr = pa->pa_podule->mod_base
208 1.1 reinoud + CONTROL_REGISTERS_POFFSET + (CONTROL_REGISTER_OFFSET << 2);
209 1.1 reinoud sc->sc_podule->irq_mask = STATUS_IRQ;
210 1.1 reinoud
211 1.1 reinoud sc->sc_ctl_reg = 0;
212 1.1 reinoud
213 1.1 reinoud status = bus_space_read_1(sc->sc_ctliot, sc->sc_ctlioh,
214 1.1 reinoud STATUS_REGISTER_OFFSET);
215 1.1 reinoud
216 1.1 reinoud printf(":");
217 1.1 reinoud /* If any of the bits in STATUS_FAULT are zero then we have a fault. */
218 1.1 reinoud if ((status & STATUS_FAULT) != STATUS_FAULT)
219 1.1 reinoud printf(" card/cable fault (%02x) -", status);
220 1.1 reinoud
221 1.1 reinoud if (!(status & STATUS_RESET))
222 1.1 reinoud printf(" (reset)");
223 1.1 reinoud if (!(status & STATUS_ADDR_TEST))
224 1.1 reinoud printf(" (addr)");
225 1.1 reinoud if (!(status & STATUS_CS_TEST))
226 1.1 reinoud printf(" (cs)");
227 1.1 reinoud if (!(status & STATUS_RW_TEST))
228 1.1 reinoud printf(" (rw)");
229 1.1 reinoud
230 1.1 reinoud printf("\n");
231 1.1 reinoud
232 1.1 reinoud /* Perhaps we should just abort at this point. */
233 1.1 reinoud /* if ((status & STATUS_FAULT) != STATUS_FAULT)
234 1.1 reinoud return;*/
235 1.1 reinoud
236 1.1 reinoud /*
237 1.1 reinoud * Enable IDE, Obey IORDY and disabled slow mode
238 1.1 reinoud */
239 1.1 reinoud sc->sc_ctl_reg |= CONTROL_IDE_ENABLE | CONTROL_IORDY
240 1.1 reinoud | CONTROL_SLOW_MODE_OFF;
241 1.1 reinoud bus_space_write_1(sc->sc_ctliot, sc->sc_ctlioh,
242 1.1 reinoud CONTROL_REGISTER_OFFSET, sc->sc_ctl_reg);
243 1.1 reinoud
244 1.1 reinoud /* Fill in wdc and channel infos */
245 1.1 reinoud sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16;
246 1.1 reinoud sc->sc_wdcdev.PIO_cap = 0;
247 1.1 reinoud sc->sc_wdcdev.channels = sc->wdc_chanarray;
248 1.1 reinoud sc->sc_wdcdev.nchannels = 2;
249 1.1 reinoud for (channel = 0 ; channel < 2; channel++) {
250 1.1 reinoud scp = &sc->simide_channels[channel];
251 1.1 reinoud sc->wdc_chanarray[channel] = &scp->wdc_channel;
252 1.1 reinoud cp = &scp->wdc_channel;
253 1.1 reinoud
254 1.1 reinoud cp->channel = channel;
255 1.1 reinoud cp->wdc = &sc->sc_wdcdev;
256 1.1 reinoud cp->ch_queue = malloc(sizeof(struct channel_queue),
257 1.1 reinoud M_DEVBUF, M_NOWAIT);
258 1.1 reinoud if (cp->ch_queue == NULL) {
259 1.1 reinoud printf("%s %s channel: can't allocate memory for "
260 1.1 reinoud "command queue", self->dv_xname,
261 1.1 reinoud (channel == 0) ? "primary" : "secondary");
262 1.1 reinoud continue;
263 1.1 reinoud }
264 1.1 reinoud cp->cmd_iot = cp->ctl_iot = &sc->sc_tag;
265 1.1 reinoud iobase = pa->pa_podule->mod_base;
266 1.1 reinoud if (bus_space_map(cp->cmd_iot, iobase +
267 1.1 reinoud simide_info[channel].drive_registers,
268 1.1 reinoud DRIVE_REGISTERS_SPACE, 0, &cp->cmd_ioh))
269 1.1 reinoud continue;
270 1.1 reinoud if (bus_space_map(cp->ctl_iot, iobase +
271 1.1 reinoud simide_info[channel].aux_register, 4, 0, &cp->ctl_ioh)) {
272 1.1 reinoud bus_space_unmap(cp->cmd_iot, cp->cmd_ioh,
273 1.1 reinoud DRIVE_REGISTERS_SPACE);
274 1.1 reinoud continue;
275 1.1 reinoud }
276 1.1 reinoud /* Disable interrupts and clear any pending interrupts */
277 1.1 reinoud scp->sc_irqmask = simide_info[channel].irq_mask;
278 1.1 reinoud sc->sc_ctl_reg &= ~scp->sc_irqmask;
279 1.1 reinoud bus_space_write_1(sc->sc_ctliot, sc->sc_ctlioh,
280 1.1 reinoud CONTROL_REGISTER_OFFSET, sc->sc_ctl_reg);
281 1.1 reinoud wdcattach(cp);
282 1.1 reinoud ihp = &scp->sc_ih;
283 1.1 reinoud ihp->ih_func = simide_intr;
284 1.1 reinoud ihp->ih_arg = scp;
285 1.1 reinoud ihp->ih_level = IPL_BIO;
286 1.1 reinoud ihp->ih_name = "simide";
287 1.1 reinoud ihp->ih_maskaddr = pa->pa_podule->irq_addr;
288 1.1 reinoud ihp->ih_maskbits = scp->sc_irqmask;
289 1.1 reinoud if (irq_claim(sc->sc_podule->interrupt, ihp))
290 1.1 reinoud panic("%s: Cannot claim interrupt %d\n",
291 1.1 reinoud self->dv_xname, sc->sc_podule->interrupt);
292 1.1 reinoud /* clear any pending interrupts and enable interrupts */
293 1.1 reinoud sc->sc_ctl_reg |= scp->sc_irqmask;
294 1.1 reinoud bus_space_write_1(sc->sc_ctliot, sc->sc_ctlioh,
295 1.1 reinoud CONTROL_REGISTER_OFFSET, sc->sc_ctl_reg);
296 1.1 reinoud }
297 1.1 reinoud
298 1.1 reinoud }
299 1.1 reinoud
300 1.1 reinoud /*
301 1.1 reinoud * Card shutdown function
302 1.1 reinoud *
303 1.1 reinoud * Called via do_shutdown_hooks() during kernel shutdown.
304 1.1 reinoud * Clear the cards's interrupt mask to stop any podule interrupts.
305 1.1 reinoud */
306 1.1 reinoud
307 1.1 reinoud void
308 1.1 reinoud simide_shutdown(arg)
309 1.1 reinoud void *arg;
310 1.1 reinoud {
311 1.1 reinoud struct simide_softc *sc = arg;
312 1.1 reinoud
313 1.1 reinoud sc->sc_ctl_reg &= (CONTROL_PRIMARY_IRQ | CONTROL_SECONDARY_IRQ);
314 1.1 reinoud
315 1.1 reinoud /* Disable card interrupts */
316 1.1 reinoud bus_space_write_1(sc->sc_ctliot, sc->sc_ctlioh,
317 1.1 reinoud CONTROL_REGISTER_OFFSET, sc->sc_ctl_reg);
318 1.1 reinoud }
319 1.1 reinoud
320 1.1 reinoud /*
321 1.1 reinoud * Podule interrupt handler
322 1.1 reinoud *
323 1.1 reinoud * If the interrupt was from our card pass it on to the wdc interrupt handler
324 1.1 reinoud */
325 1.1 reinoud int
326 1.1 reinoud simide_intr(arg)
327 1.1 reinoud void *arg;
328 1.1 reinoud {
329 1.1 reinoud struct simide_channel *scp = arg;
330 1.1 reinoud irqhandler_t *ihp = &scp->sc_ih;
331 1.1 reinoud volatile u_char *intraddr = (volatile u_char *)ihp->ih_maskaddr;
332 1.1 reinoud
333 1.1 reinoud /* XXX - not bus space yet - should really be handled by podulebus */
334 1.1 reinoud if ((*intraddr) & ihp->ih_maskbits)
335 1.1 reinoud wdcintr(&scp->wdc_channel);
336 1.1 reinoud
337 1.1 reinoud return(0);
338 1.1 reinoud }
339